Problems getting insert statment to work

Hi all

I am having trouble getting the following

  $add_grade=("INSERT INTO score ( right, wrong) VALUES ( '$right', '$wrong')");
     $result_grade = mysqli_query($dbcon, $add_grade);

in the following script.
I have attempted running it in different part’s of the script but while the code script works as expected but the insertion is not made in to the database. If possible could some one point out to me where I am going wrong.

<?php
session_start();
   	$_SESSION['right'] = 0;
    $_SESSION['wrong'] = 0;

?>

	
<?php
	
	if(isset($_POST['submit'])) {
	//echo $user_id;
    foreach($_POST['test_id'] as $test_question_id => $test_response) {
		$test_question_id = (int) $test_question_id;
        $test_response = (int) $test_response;
        require_once ('mysqli_connect.php');
        $test_response= "SELECT COUNT(*) AS right FROM survey WHERE survey_num = {$survey_id} AND test = {$test_response}";
        $result = mysqli_query($dbcon, $test_response);
       while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        if($row['right']) {
			 $_SESSION['right']++;
        }else{
		
		    $_SESSION['right']++;
        }

    }
	
     $add_grade=("INSERT INTO score ( right, wrong) VALUES ( '$right', '$wrong')");
     $result_grade = mysqli_query($dbcon, $add_grade);
  }
}
?>
<?php

echo "Number of of right answers :       ".$_SESSION['right'];
echo '<br>';
echo "Number of of wrong answers :  ".$_SESSION['wrong'];

?>

I don’t see the variables $right and $wrong defined anywhere.

Even when i had the following code in to the script:


$right = $_SESSION['right'];
$wrong = $_SESSION['wrong'];

before the sql statement

nothing changes, the script runs through, shows me the echo statements but nothing it inserted into the database.

umm are you sure the session variables do exist? Use var_dump on every variable to see if one is empty or else.

i have being using print_r and it shows me that that the session variables do exist.

Have you checked for errors after you run the query? I don’t use mysqli so I’m not sure how you’d do it. If you run the query in phpmyadmin or whatever you use, does it work without errors? Should the quotes be around the values?


     $result_grade = mysqli_query($dbcon, $add_grade) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);

Also, you use $survey_id in your first query (where you’re totting up the right and wrong answers) but I don’t see that defined anywhere - does that come from elsewhere in the code? If you add an echo() within the loop as you go through the results from that query, does it display what you expected? I’m a newbie to this, but doesn’t “select count(*)” mean you’ll only get a single result anyway, so looping through the results isn’t going to help? And I don’t understand this bit as you assess the results:


        if($row['right']) { 
			 $_SESSION['right']++;
        }else{
		    
		    $_SESSION['right']++;
        } 

Is it a typo in the board posting, or in actual code, that you increment ‘right’ whatever the answer is?

Every displays as expected and so does the count query. The error i get once i entered your code was:

As line 1 of that script is the tag for opening php ,

<?php 

i can’t understand how this would be a sql syntax error?

That two rights are a typo, the first should be right and the second wrong

Sorry !

add error handling to mysqli:

mysqli_report(MYSQLI_REPORT_STRICT);

I got it working but thanks for all your help !!!