Hi I have a database with the following table:
Answers - QuestionId, Answer1,answer2, answer3, answer4, CorrectAnswer
And a form with 5 questions and four possible answers for each. The answer is chosen by choosing one of the four radio buttons for each question. What i'm trying to do is match the value of the radio button to the value in the column 'CorrectAnswer' to show the user if they got the answer right or wrong.
This is my code s far:
<?php
if(!empty($POST)){
$questions = $POST['QuestionId'];
foreach($questions as $k => $v){
$r = mysql_query("SELECT CorrectAnswer FROM answers WHERE questionID = $k") or die(mysql_error());
$correct = mysql_fetch_row($r);
var_dump($correct);
if($v == $correct['CorrectAnswer']){
print "Answer for question $k is correct!<br>";
}
else{
/* print "radio button chosen = $v "; */
print "Answer for question $k is wrong!<br>";
}
}
}
else{
?>
and my radio buttons are coded as:
<form method="post" action="">
<input name="QuestionId[1]" type="radio" value="1">
<input name="QuestionId[1]" type="radio" value="2">
<input name="QuestionId[1]" type="radio" value="3">
<input name="QuestionId[1]" type="radio" value="4">
<input name="QuestionId[2]" type="radio" value="1">
<input name="QuestionId[2]" type="radio" value="2">
<input name="QuestionId[2]" type="radio" value="3">
<input name="QuestionId[2]" type="radio" value="4">
<input type="submit" value="Get Marks"/>
</form>
<?php
}
But the answers always came back wrong even when they were ment to be correct I done a var_dump($correct) and got the results:
array(1) { [0]=> string(1) "3"}
array(1) { [0]=> string(1) "4"}
array(1) { [0]=> string(1) "1"}
array(1) { [0]=> string(1) "2"}
Because they all have [0] in it seems that the problem lies here. I put in a print to screen to get the value in $k and that works correctly and gives the correct question number but just not with the radio buttons.
Any ideas? 🙂