I have the following code which scores a quiz after it has been submitted. The cript works but it doesnt insert the information into the databse. I tested the quiery 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;
?>
Problem with sql insert quesry
Moderator: General Moderators
Re: Problem with sql insert quesry
You are defining a query variable but you never execute it. If you want to send this command into the mysql database you gotta do this
In order to verify the insert did indeed happen and no error occured you can do
Code: Select all
$query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')";
mysql_query($query);Code: Select all
$query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')";
if(!mysql_query($query)){
echo "Mysql Error Occured: ".mysql_error();
exit;
}Re: Problem with sql insert quesry
Also is there a way i can create a percentage for the scores? I know i need to do an answer count and a question count and divide, but im not sure how i would do it
Re: Problem with sql insert quesry
ykarmi wrote:You are defining a query variable but you never execute it. If you want to send this command into the mysql database you gotta do this
In order to verify the insert did indeed happen and no error occured you can doCode: Select all
$query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')"; mysql_query($query);
Code: Select all
$query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')"; if(!mysql_query($query)){ echo "Mysql Error Occured: ".mysql_error(); exit; }
Thanks, this worked!