Hi.
I am working on a "timed test" for college. I have picked up a piece of code from the net, and am tweaking it.
The flow is: User logs in, a set of questions is displayed one by one (with a timer), and once he finishes (or time is up) a score is displayed.
I am able to get the user to log in, but when he clicks on the link to start the quiz, I get this:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
I am running Apache 2.2.3, PHP 5.1.6, phpMyAdmin 2.9.0.1 (MySQL client version: 5.0.24a) - all part of XAMPP for Windows 1.5.4a. The server machine runs on Windows 2K Pro.
Any help will be greatly appreciated, I have been breaking my head on this for a week now.
Thanks in advance!
I have posted the "offender" code below:
trivia.php
<?php
require("config.php");
if ($logged_in == 0) {
die('Sorry you are not logged in, this area is restricted to registered members. <a href="login.php">Click here</a> to log in.');
}
# check number of plays per day
if ($_SESSION['tests_count']==5) {
$template = "template5.html";
if(!$output = file($template)) die("Couldn't open the template file ($template). Please check and make sure that this file is where it's supposed to be.");
$resultoutput="";
$formoutput="";
$output = implode("\n", $output);
$output = str_replace("%%RESULT%%", $resultoutput, $output);
$output = str_replace("%%NICKNAME%%", $_SESSION['nickname'], $output);
$output = str_replace("%%FORM%%", $formoutput, $output);
echo $output;
exit();
}
# check number of questions per play
if (!isset($_SESSION['questions_count'])) {
$_SESSION['questions_count']=1;
} else {
if($_SESSION['questions_count']<$questions_test) {
$_SESSION['questions_count']++;
}
else {
unset($_SESSION['questions_count']);
$update_login = "UPDATE registration SET test_day=test_day+1 WHERE nickname = '".$_SESSION['nickname']."'";
mysql_query($update_login) or die(mysql_error());
$SqlTestcheck="SELECT test_day FROM registration WHERE nickname='".$_SESSION["nickname"]."'";
$Testcheck=mysql_query($SqlTestcheck) or die(mysql_error());
$Testinfo = mysql_fetch_array($Testcheck);
$_SESSION['tests_count'] = $Testinfo["test_day"];
$template = "template3.html";
if(!$output = file($template)) die("Couldn't open the template file ($template). Please check and make sure that this file is where it's supposed to be.");
$resultoutput="";
$formoutput="";
$output = implode("\n", $output);
$output = str_replace("%%RESULT%%", $resultoutput, $output);
$output = str_replace("%%NICKNAME%%", $_SESSION['nickname'], $output);
$output = str_replace("%%PLAYSLEFT%%", ($total_tests - $_SESSION['tests_count']), $output);
$output = str_replace("%%FORM%%", $formoutput, $output);
echo $output;
mysql_close();
exit();
}
}
#$template is the template file the script will use to
#format the form.
$template = "formtemplate.html";
#load the template file
if(!$output = file($template))
die("Couldn't open the template file ($template). Please check and make sure that this file is where it's supposed to be.");
$output = implode("\n", $output);
#calculate number of questions in the database
$SqlTotalQuestions=mysql_query("SELECT max(id) AS total FROM question");
$RowTotalQuestions=mysql_fetch_array($SqlTotalQuestions);
$TotalQuestions=$RowTotalQuestions["total"];
#Pick a random question
$RandomQuestion=rand(1,$TotalQuestions);
$SqlQuestionSelected=mysql_query("SELECT id, question FROM question WHERE id=$RandomQuestion")
or die ("Error... Cant select a valid question");
#populate the random question
$RowQuestionSelected=mysql_fetch_array($SqlQuestionSelected);
$TheQuestion = htmlspecialchars($RowQuestionSelected["question"]);
$IdTheQuestion = $RowQuestionSelected["id"];
#select/populate choices array
$SqlA = "SELECT * FROM answer WHERE answer.idq=$IdTheQuestion";
$regA=mysql_query($SqlA) or die(mysql_error());
$j=0;
while($Answers = mysql_fetch_array($regA) ) {
$IdChoices[$j]=$Answers["id"];
$Choices[$j]=$Answers["answer"];
$j++;
}
#build form output for CHOICES
$nChoices=sizeof($Choices);
$choiceoutput="";
$Text="";
for($i=0; $i<$nChoices; $i++) {
$Text='<input type="radio" name="answer" value="' . $IdChoices[$i] . '"';
if($i==0) {
$First=" CHECKED ";
$Text.=$First;
}
$Text.="> ". $Choices[$i] . "</input>";
$choiceoutput.= $Text."<br />";
}
#close the database connection, we just dont need anymore
mysql_close();
$time = time();
$formoutput ="<input type=\"hidden\" name=\"question\" value=\"" . $TheQuestion . "\">";
$formoutput .="<input type=\"hidden\" name=\"idquestion\" value=\"" . $IdTheQuestion . "\">";
$formoutput .="<input type=\"hidden\" name=\"time\" value=\"" . $time . "\">";
$formoutput .= $choiceoutput;
$resultoutput=($_REQUEST["resultoutput"]=="") ? "" : $_REQUEST["resultoutput"];
$output = str_replace("%%RESULT%%", $resultoutput, $output);
$output = str_replace("%%NUMQUESTION%%", $_SESSION['questions_count'], $output);
$output = str_replace("%%QUESTION%%", $TheQuestion, $output);
$output = str_replace("%%FORM%%", $formoutput, $output);
echo $output;
exit();
?>