我正在使用此代码来检查答案是否正确:
<?php
$con=mysqli_connect("localhost","root","","teachme");
//check connection
if (mysqli_connect_errno()) {
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
$query = "SELECT question, answer, explanation FROM grade8u1 WHERE id BETWEEN 1 AND 10 ORDER BY RAND()";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_row($result);
echo ("<b>Question</b>: $row[0]<br>");
echo ("<b>Answer</b>: $row[1]<br>");
echo ("<b>Explanation</b>:<br> $row[2]<br>");
if (isset($_POST['answer'])) {
$answer = $_POST['realanswer'];
if ($_POST['answer'] == $answer) {
echo "<br>";
echo "Last answer was correct";
}
else {
echo "<br>";
echo "Last answer was incorrect";
}
}
?>
<form action="random.php" method="post">
<input type="hidden" name="realanswer" value="<?php echo $row[1]; ?>">
Answer: <input type="text" name="answer">
<input type="submit" value="Submit">
</form>
存在一个问题,因为任何人都可以检查或编辑隐藏字段的值。我尝试了其他方法,例如使用会话,但每次用户按下提交时它们都会被替换。谢谢,汤姆。