如何查看测验中用户的选择是否正确? [英] How can I see if the user's choice in the quiz is correct?
问题描述
我正在尝试检查用户选择的答案是否正确,但是不能正常工作:
I am trying to check if the answer that the user chose was correct, but it doesn't work properly:
<form method="post">
<?php
$question = mysql_query("SELECT * FROM `questions`");
$stat = mysql_fetch_assoc($question);
$num = mysql_num_rows($question);
$questionid = 0;
for($i=0;$i<=$num;$i++)
{
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$stat = mysql_fetch_assoc($question);
//if($stat['answer'] == null
echo $stat['question'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer1']."<br />";
echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer2'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer3'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer4'] . '<br />';
$questionid++;
$x = $x+1;
}
echo $result;
?>
<input type="submit" name="go" value="Go" />
</form>
那部分基本上是:
$x = 1;
$question = mysql_query("SELECT * FROM `questions`");
$stat = mysql_fetch_assoc($question);
$num = mysql_num_rows($question);
if(isset($_POST['go']))
{
for($i=0;$i<$num;$i++)
{
if ($_POST['ans'.$x] == $row['correct'])
{
$result = $result + 1;
}
$x = $x + 1;
}
}
由于某种原因,它无法正确发布结果,并且我认为代码有问题,希望能提供帮助.
For some reason it doesn't post the results properly and I think something is wrong with the code, would appreciate help.
推荐答案
这是帮助您的另一种尝试.
Here's another attempt at helping you.
我实际上写了一个完整的解决方案",并在此过程中发现了代码中的一些小错误-以及一些我无法理解的东西.
I actually wrote a "complete solution", and in the process discovered a few little bugs in your code - plus some things I just couldn't understand.
主要错误:您所有的单选按钮都具有相同的值($ x),因此无论您对问题1按下哪个按钮,答案都为"1",依此类推.您做了其他一些事情,但我做不到弄清楚-所以我要做的是创建一个简单的流程-一页询问问题,另一页评估结果.
Principal bug: all your radio buttons have the same value ($x), so no matter what button you press for question 1, the answer is "1", etc. There were other things you did that I could not quite figure out - so what I did instead was create a simple flow - one page that asks the questions, and another that evaluates the results.
问题页面(我混淆了数据库的访问参数-不,我不使用"password"作为密码!):
Question page (I obfuscated access parameters for my database - no, I don't use "password" as my password!):
<html>
<body>
<form action="./evaluate.php" method="post">
<?php
$server = mysql_connect ('localhost', 'username, 'password');
mysql_select_db("questionnaire", $server);
$question = mysql_query("SELECT * FROM `Questions`;");
$x = 0;
while ($row = mysql_fetch_assoc($question))
{
echo $row['question'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value=1 />' .$row['answer1'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value=2 />' .$row['answer2'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value=3 />' .$row['answer3'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value=4 />' .$row['answer4'] . '<br />';
$x = $x + 1;
}
mysql_close($server);
?>
<input type="submit" name="Submit" value="Submit" />
<br>
</form>
</body>
</html>
和valuate.php: 我稍稍更改了代码以使输出更干净",并添加了红色/绿色以显示已正确和错误回答的问题.显然,如果需要,您可以将这些事情做得更远……
and evaluate.php: I changed the code a bit to make the output "cleaner", and add a red/green touch to show questions that had been answered correctly and incorrectly. Obviously you can take these things much further if you want...
<html>
<body>
<?php
$server = mysql_connect ('localhost', 'username', 'password');
mysql_select_db("questionnaire", $server);
$question = mysql_query("SELECT * FROM `Questions`;");
$x = 0;
$score = 0;
while ($row = mysql_fetch_assoc($question))
{
echo $row['question'] . '?<br />';
$answered = $row['answer'.$_POST['a'.$x]] ;
$correct = $row['correct'] ;
if ($answered == $correct ) {
$score++;
$acolor = 'green' ;
}
else {
$acolor = 'red' ;
}
echo 'you answered <font color=' . $acolor . '>' . $answered . '<font color=black> <br />';
echo 'the correct answer was ' . $correct . '<br />' ;
echo '-------------------------------------- <br />' ;
$x = $x + 1;
}
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';
mysql_close($server);
?>
</body>
</html>
这产生了预期的结果(对于我做出的一个简单的三个问题的多项选择).让我知道它是否对您有用!
This produced (for a simple three-question multiple choice I made) the expected results. Let me know if it works for you!
这篇关于如何查看测验中用户的选择是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!