Problem with sql insert quesry

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sm_558
Forum Newbie
Posts: 5
Joined: Sun Apr 06, 2008 5:52 pm

Problem with sql insert quesry

Post by sm_558 »

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:&nbsp;&nbsp;</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>)&nbsp;".$question."<br/>";
if($selectedAnswer == $rightanswer)
{
echo "<br><b>Correct:&nbsp;&nbsp;</b>".$rightanswer;
$noCorrect++;
}

else {
echo "<br><b>Incorrect:&nbsp;&nbsp;</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;


?>
ykarmi
Forum Commoner
Posts: 35
Joined: Mon Oct 30, 2006 4:45 pm

Re: Problem with sql insert quesry

Post by ykarmi »

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

Code: Select all

$query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')";
mysql_query($query);
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')";
if(!mysql_query($query)){
echo "Mysql Error Occured: ".mysql_error();
exit;
}
sm_558
Forum Newbie
Posts: 5
Joined: Sun Apr 06, 2008 5:52 pm

Re: Problem with sql insert quesry

Post by sm_558 »

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
sm_558
Forum Newbie
Posts: 5
Joined: Sun Apr 06, 2008 5:52 pm

Re: Problem with sql insert quesry

Post by sm_558 »

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

Code: Select all

$query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')";
mysql_query($query);
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')";
if(!mysql_query($query)){
echo "Mysql Error Occured: ".mysql_error();
exit;
}

Thanks, this worked! :)
Post Reply