我将显示一些从我的数据库中调用的随机问题,并使用代码检查答案:
问题.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>
如何更改上述代码以使整个系统正常工作?这些代码在没有随机的情况下起作用,并且问题后面是升序。