Jump to content

Problem with sql insert query


akramm

Recommended Posts

Hi Guys...... I would appreciate ur help on this..

 

 

I have the following code which scores a quiz after it has been submitted. The script works but it doesnt insert the information into the databse. I tested the query in MySQL front and it does work, so its something wrong with my PHP script. Ive tried moving the query around but it does not work.

 

<?php
session_start(); 
// This script handle all the user commands.
require_once ("database.php");
require_once ("globals.php");
require_once ("loginlib.php");
$usertype = Getusertype ();
$username = Getusername ();
$userid = Getuserid ();
$title = $g_system_name . " -- Dashboard";

ShowHeader ($title);
ShowLinks (5);

?>
<?

//require_once ('mysql_connect.php'); // Connect to the db.
mysql_connect("localhost", "root", "") or die ("Unable to connect to the server");
// select the database or return an error message
mysql_select_db("nluser") or die ("Unable to connect to the database"); 

$total = $_POST['total'];

$quizID = $_SESSION['id'];
$noCorrect = 0;
echo "<b>Quiz ID:  </b>".$quizID;


$result = mysql_query("SELECT * FROM questions WHERE quizID='$quizID'"); 
if(mysql_numrows($result))
{
while($rows = mysql_fetch_row($result))
{


//$rows = mysql_fetch_row($result);


//print_r($row);

//echo "start of the while loop - under the form<br>";
$qid=$rows[0];
$question=$rows[1];


$question_no=$rows[2];

$rightanswer=$rows[3];

$selection = "answer".$question_no;
//echo "assign values to array<br>";
$selectedAnswer = $_POST[$selection];
echo "<br><br><b>".$question_no."</b>) ".$question."<br/>";
if($selectedAnswer == $rightanswer)
{
echo "<br><b>Correct:  </b>".$rightanswer;
$noCorrect++;
}

else {
echo "<br><b>Incorrect:  </b>".$selectedAnswer;
}



}
print("<br><br><b>Total correct: </b>".$noCorrect." out of ".$total);

$query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')";



}

//echo $query;


?>

 

 

Could you help me get this to work!?

 

Many Thanks!

 

mod edit : use

...

tags around your code

Link to comment
https://forums.phpfreaks.com/topic/100030-problem-with-sql-insert-query/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.