Good Afternoon,
I am preparing some online exams using PHP. I have my code set up as follows:
$message="";
$numone=1;
$numtwo=0;
date("Y-m-d H:i:s");
$testdate=date("Y-m-d H:i:s");
if (isset( $actionflag ) && $actionflag=="q1" )
{
if (($calcA =="") && ($calcB =="") && ($calcC =="") && ($calcD =="") && ($calcE =="")) {
$message .="You forgot to answer the question!";
}
mysql_query("INSERT INTO test ( id, username, password, testdate )
VALUES ( '$session[id]', '$session[username]', '$session[password]', '$testdate' )");
if (($calcE == "answerE") && ($calcB =="") && ($calcC =="")
&& ($calcD =="") && ($calcA ==""))
{
mysql_query("UPDATE test set questionone = $numone WHERE id = $session[id]");
}
else
{
mysql_query("UPDATE test set questionone = $numtwo WHERE id = $session[id]");
}
if ( $message == "" )
{
header("Location: http://www.careinternet.net/moduleone/q2.php?".SID );
}
}
}
This code checks for an entry and if that entry is present it inserts a value of 1 into the database. Otherwise the answer is wrong and the student gets a 0 inserted into the database. My problem is with the error checking. I have set up the exam to default to another page if there is already an entry in the present question. This way no one can go back and change their answers. However this has developed a small bug and although it probably won't be a problem I would like to fix it.
If the student hits the submit button once the error message will come up but then the code also inserts a zero into the database. If they hit the submit again it the query code will recognize an entry and move default to the error page telling the student they have already answered the question.
My question is how can I get the database not to update unless an actual answer has been inserted in the form?