我的数据库表包含以下字段:
id, question ,answer ,answer2 ,answer3 ,answer4
正确答案位于answer
字段。
问题在于,当我选择正确的答案并提交时,它会将其显示为错误的答案。 这是HTML代码:
$sql=mysql_query("SELECT * FROM `qs_ans_php`") or die(mysql_error());
$questionNumber = 1;
<td>Question:<?php echo $questionNumber; ?></td><td><?php echo $rt['question'];?></td>
</tr>
<tr>
<td><input type="radio" name="ans[<?php echo $rt['id'];?>]" value="a" /><?php echo $rt['answer'];?></td>
</tr>
<tr>
<td><input type="radio" name="ans[<?php echo $rt['id'];?>]" value="b" /><?php echo $rt['answer2'];?></td>
</tr>
<tr>
<td><input type="radio" name="ans[<?php echo $rt['id'];?>]" value="c" /><?php echo $rt['answer3'];?></td>
</tr>
<tr>
<td><input type="radio" name="ans[<?php echo $rt['id'];?>]" value="d" /><?php echo $rt['answer4'];?></td>
这是php代码:
$correctAnswers = 0;
$wrongAnswers = 0;
$idList = join (',', array_map('intval', array_keys($_POST['ans'])));
$sql = mysql_query("SELECT `id`,`answer` FROM `qs_ans_php` WHERE `id` IN ($idList) ") or die(mysql_error());
while(list($id, $correct) = mysql_fetch_row($sql))
{
if ($correct == $_POST['ans'][$id])
{
$correctAnswers =+1;
}
else
{
$wrongAnswers +=1;
}
}
?>
<html>
<body>
<p>
<?php $numberOfQs = $correctAnswers + $wrongAnswers;
$score = round(($correctAnswers / $numberOfQs) * 100);
?>
Correct Answers: <?php echo $correctAnswers; ?> <br>
Wrong Answers: <?php echo $wrongAnswers; ?> <br>
Score: <?php echo $score; ?>