We help IT Professionals succeed at work.

[php|mysql] Issue with Insert Statement

457 Views
Last Modified: 2012-02-14
I have an insert statement that is not actually inserting anything into my database. It is part of a larger conditional statement that is checking for form data with GET, inserting that data to a data base and then selecting the data from the table that has the page data in it.

Below is the insert:
mysql_query ("INSERT INTO `foo` test_id, res_step_number, res_step_sub_number, res_step_outcome, res_step_notes, rest_test_taker) 
                    VALUES ('$_SESSION[test_id]','$_REQUEST[test_question]','$_REQUEST[test_question_sub]','$_REQUEST[question]','$_REQUEST[notes]','$_SESSION[test_taker]')");

Open in new window


And below is one example from my conditional statement with the insert:
elseif ($_REQUEST['test_question'] > 1 && $_REQUEST['test_question'] <= $_SESSION['total_q']) {

       // Load previous test data to the database
      mysql_query ("INSERT INTO `foo` test_id, res_step_number, res_step_sub_number, res_step_outcome, res_step_notes, rest_test_taker) 
                    VALUES ('$_SESSION[test_id]','$_REQUEST[test_question]','$_REQUEST[test_question_sub]','$_REQUEST[question]','$_REQUEST[notes]','$_SESSION[test_taker]')");

      // Get the next question
      $get_question = "SELECT test_id, step_number, step_number_sub, step_required, step_description, step_outcome, question_primary 
                      FROM create_test_questions
                      WHERE question_primary = '$_REQUEST[new_question_id]'
                      LIMIT 1";
      $result = mysql_query($get_question);
      $row = mysql_fetch_row($result);

      // Assign variables to populate question
      $test_id                          = $row['0'];
      $step_number             = $row['1'];
      $step_number_sub     = $row['2'];
      $step_required            = $row['3'];
      $step_description       = $row['4'];
      $step_outcome            = $row['5'];
      $test_question_id        = $row['6'];

      // Prepare variables for grabbing next question
      $test_question = $_REQUEST['test_question'] + 1;
      $new_question_id = $test_question_id + 1;

Open in new window


Let me know if you need anything else!
Comment
Watch Question

Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Sorry for the stupid question, but have you session_start() command at the top of the script?
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
"MySQL typically prefers column names be put in backquotes as such: "

MySQL does not actually care unless the column name is a reserved word, so

INSERT INTO myTable ( aaa, bbb ) VALUES( 1, '2012-01-01' )

will work ok, but

INSERT INTO myTable ( aaa, date ) VALUES( 1, '2012-01-01' )

will fail, but putting backticks around the problem column name will make the statement succeed

INSERT INTO myTable ( aaa, `date` ) VALUES( 1, '2012-01-01' )

Author

Commented:
bportlock - Thank you so much for your help! That cleaned things up quite a bit!  One quick question, if I have my MySQL connection open, can I do something like this:

 // Load previous test data to the database
      $sql_test_id              = mysql_real_escape_string( $_SESSION['test_id']);
      $sql_test_question        = mysql_real_escape_string( $_REQUEST['test_question']);
      $sql_test_question_sub    = mysql_real_escape_string( $_REQUEST['test_question_sub']);
      $sql_question             = mysql_real_escape_string( $_REQUEST['question']);
      $sql_notes                = mysql_real_escape_string( $_REQUEST['notes']);
      $sql_test_taker           = mysql_real_escape_string( $_REQUEST['test_taker']);

      // Load previous test data to the database
      $query  = "INSERT INTO foo
                 (  test_id,
                    res_step_number,
                    res_step_sub_number,
                    res_step_outcome,
                    res_step_notes,
                    rest_test_taker
                 )
                 VALUES (
                    '$sql_test_id',
                    '$sql_test_question',
                    '$sql_test_question_sub',
                    '$sql_question',
                    '$sql_notes',
                    '$sql_test_taker'
                 )";
      $result = mysql_query($query);

Open in new window


I tried your code and then tried a few other tweaks to it and nothing seemed to load the data into my table.  

 jgbauer -
Thanks for the feedback. I tried using the PHP_EOL and nothing showed. I did a var_dump of my query, and it resulted in the following output:

string(472) "INSERT INTO foo ( test_id, res_step_number, res_step_sub_number, res_step_outcome, res_step_notes, rest_test_taker ) VALUES ( '75', '7', '0', 'pass', 'TEst', 'Peter' )"

Open in new window


Thanks so much for all your help!
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
PERFECT!  That fixed it!  I had a typo in one of my field labels, and it works perfect now. Thanks so much!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.