我打算从我的数据库中显示一些随机问题,并使用代码检查答案:
question.php:
<html>
<body>
<h3>Please answer all questions.</h3><br><br>
<form action="phpinfo.php" method="post">
<?php
//connect to db
$server = mysql_connect ('localhost', 'username', 'password');
mysql_select_db("mc", $server);
$question = mysql_query("SELECT * FROM `question` ORDER BY RAND() LIMIT 20;"); // 60 questions in my database
$x = 0;
while ($row = mysql_fetch_array($question))
{
echo "Question: ".$row['q_do'] . '<br />'; //q_do are my questions
echo '<input type="radio" name="a'.$x.'" value=a />' ,"A ".$row['a'] . '<br />'; // answer a
echo '<input type="radio" name="a'.$x.'" value=b />' ,"B ".$row['b'] . '<br />'; // answer b
echo '<input type="radio" name="a'.$x.'" value=c />' ,"C ".$row['c'] . '<br />'; // answer c
echo '<input type="radio" name="a'.$x.'" value=d />' ,"D ".$row['d'] . '<br />'; // answer d
$x++;
};
mysql_close($server);
?>
<input type="submit" name="Submit" value="Submit" /> //go to next page
<br>
</form>
</body>
</html>
phpinfo.php的:
<html>
<body>
<?php
//connect to db
$server = mysql_connect ('localhost', 'username', 'password');
mysql_select_db("mc", $server);
$question = mysql_query("SELECT * FROM `question` ;");
$x = 0;
$score = 0;
while ($row = mysql_fetch_array($question)) //do not know how to call the questions did in previous page since they are random
{
echo $row['q_do'] . '<br />';
$answered = $row[$_POST['a'.$x]] ; // the answers
$correct = $row['corr_ans'] ;
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++;
}
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';
mysql_close($server);
?>
</body>
</html>
如何更改上述代码以使整个系统正常工作? 这些代码适用于没有随机的问题,而问题则按升序排列。
答案 0 :(得分:0)
解决问题的最简单方法是简单地将问题的id附加到每个问题的value属性,如下所示:
echo '<input type="radio" name="a'.$x.'" value="a'.$row['id'].'" />' ,"A ".$row['a'] . '<br />';
这将为您提供每个问题的唯一答案值,您可以使用该值来获取问题的ID,然后从数据库中提取问题。
因此,如果您对其中一个答案的价值是&#39; b7&#39;你知道用户用选项b回答了问题7。
要从字符串中获取该ID,例如&#39; b7&#39;你可以使用以下内容:
$questionId = filter_var($fullAnswer, FILTER_SANITIZE_NUMBER_INT);
因为现在你知道问题是什么,你可以使用id从数据库中获取它,并对该问题进行剩余的处理。
因此,在您的服务器端脚本中,您必须将循环更改为简单地迭代20次(因为这已经提交了多少个问题)。这是您进行所有必需更改后两个文件的外观:
question.php:
<html>
<body>
<h3>Please answer all questions.</h3><br><br>
<form action="phpinfo.php" method="post">
<?php
//connect to db
$server = mysql_connect ('localhost', 'username', 'password');
mysql_select_db("mc", $server);
$question = mysql_query("SELECT * FROM `question` ORDER BY RAND() LIMIT 20;"); // 60 questions in my database
$x = 0;
while ($row = mysql_fetch_array($question))
{
echo "Question: ".$row['q_do'] . '<br />'; //q_do are my questions
echo '<input type="radio" name="a'.$x.'" value="a'.$row['id'].'" />' ,"A ".$row['a'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value="b'.$row['id'].'" />' ,"B ".$row['b'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value="c'.$row['id'].'" />' ,"C ".$row['c'] . '<br />';
echo '<input type="radio" name="a'.$x.'" value="d'.$row['id'].'" />' ,"D ".$row['d'] . '<br />';
$x++;
};
mysql_close($server);
?>
<input type="submit" name="Submit" value="Submit" /> //go to next page
<br>
</form>
</body>
</html>
phpinfo.php的:
<html>
<body>
<?php
//connect to db
$server = mysql_connect ('localhost', 'username', 'password');
mysql_select_db("mc", $server);
$x = 0;
$score = 0;
// Make 20 iterations to process the 20 answers
for ($i=0; $i<=19; $i++)
{
$fullAnswer = $row[$_POST['a'.$x]] ;
// Find the question id
$questionId = filter_var($fullAnswer, FILTER_SANITIZE_NUMBER_INT);
// Find the answer
$answer = substr($fullAnswer, 0, 1);
// get the question for the answer of current iteration
$result = mysql_query("SELECT * FROM `question` WHERE id=$questionId LIMIT 1;");
$question = mysql_fetch_assoc($result);
echo $question['q_do'] . '<br />';
$correct = $question['corr_ans'] ;
if ($answer == $correct ) {
$score++;
$acolor = 'green' ;
}
else {
$acolor = 'red' ;
}
echo 'you answered <font color=' . $acolor . '>' . $answer . '<font color=black> <br />';
echo 'the correct answer was ' . $correct . '<br />' ;
echo '-------------------------------------- <br />' ;
$x++;
}
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';
mysql_close($server);
?>
</body>
</html>
简单:)
这可能会奏效,但我没有测试过。
大家知道,使用mysql_query是一个坏主意。它是折旧的。你应该使用PDO。查找有关如何使用它的PHP文档,然后更改所有数据库内容以在代码中使用它。
祝你好运。