Hello,
I'm fairly new to all this php and would like some help.
Basically I'm creating a quiz website, I have my questions in a MySQL database which works fine but when my web page tries to validate an examinee's answer with the correct answer in the database it seems to be checking the next question rather than the one presented on the web page.
Here's all the code for the question page:
<?php
session_start();
$SESSION['qnum']++;
print 'current qnum is ' .$SESSION['qnum'];
include "conn.inc.php";
?>
<html>
<head>
<title>Matthews test</title>
</head>
<body>
<?php
$query = "SELECT * FROM question where questionid = '" . $_SESSION['qnum'] . "' - 0 ;";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
$SESSION['correctanswer']= $row['correctanswer'];
print 'correct answer is ' .$SESSION['correctanswer'];
?>
<form action="<?php echo $PHP_SELF;?>" method="post" name="question2">
<table>
<tr>
<td>
Here is your first question...<br></br><?php echo $row['questiontext']; ?>?<br></br>
<input type="radio" name="radQ1" value="<?php echo $row['correctanswer']; ?>" /><?php echo $row['correctanswer']; ?><br></br>
<input type="radio" name="radQ1" value="<?php echo $row['incorrectanswer1']; ?>" /><?php echo $row['incorrectanswer1']; ?><br></br>
<input type="radio" name="radQ1" value="<?php echo $row['incorrectanswer2']; ?>" /><?php echo $row['incorrectanswer2']; ?><br></br>
<input type="radio" name="radQ1" value="<?php echo $row['incorrectanswer3']; ?>" /><?php echo $row['incorrectanswer3']; ?><br></br>
<input type="radio" name="radQ1" value="<?php echo $row['incorrectanswer4']; ?>" /><?php echo $row['incorrectanswer4']; ?><br></br>
</td>
</tr>
</table>
<br></br>
<input type="submit" name="submit" value="Submit my answer..."
<?php
if
($POST['radQ1'] == $SESSION['correctanswer']) {
$query_update = "UPDATE dummy SET correctnumber = correctnumber + 1";
$result_update = mysql_query($query_update)
or die(mysql_error());
$SESSION['qnum']++;
} else {
$query_update = "UPDATE dummy SET incorrectnumber = incorrectnumber + 1";
$result_update = mysql_query($query_update)
or die(mysql_error());
$SESSION['qnum']++;
}
?>"/>
</form>
</body>
</html>
So what I hoped to have was one page that would present a question, the examinee would answer it, the answer would be checked against the database, if it is correct increment 'correctnumber' on the 'dummy' table (this is for testing purposes) and if incorrect increment the 'incorrectnumber' on the same table.
An example of what it is doing is:
Question 1 is presented, if I answer correctly it increments (wrongly) 'incorrectnumber'. However, when question 1 is presented and I give the answer to question 2 it increments 'correctanswer'; then when question 2 is presented if I answer with question 3's answer the 'correctnumber' is (wrongly again) incremented and so on. So it seems to be submitting my answer to the question I can see on screen to the next question in the database.
So, my question is, how can I answer the question on-screen with a radio button answer and have that validated against the correct question, then once that's right or wrong, record the score (in 'correctnumber' or 'incorrectnumber'), then move on to the next question?
I have dozens of questions and eventually they will be in a random order, so I can't just have loads of php files with each question hard-coded into them.
Sorry if this is a bit long, but hopefully it's clear to you if you can help.
Many thanks,
Matthew