2

我将显示一些从我的数据库中调用的随机问题,并使用代码检查答案:

问题.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>

如何更改上述代码以使整个系统正常工作?这些代码在没有随机的情况下起作用,并且问题后面是升序。

4

1 回答 1

0

解决问题的最简单方法是简单地将问题的 id 附加到每个问题的 value 属性中,如下所示:

   echo '<input type="radio" name="a'.$x.'" value="a'.$row['id'].'" />' ,"A ".$row['a'] . '<br />';

这将为您提供每个问题的唯一答案值,您可以使用它来获取问题的 ID,然后从数据库中提取问题。

因此,如果您对其中一个答案的值是“b7”,则您知道用户使用选项 b 回答了问题 7。

要从像 'b7' 这样的字符串中获取该 ID,您可以使用以下命令:

$questionId = filter_var($fullAnswer, FILTER_SANITIZE_NUMBER_INT);

因为现在您知道问题是什么,所以您可以使用 id 从数据库中获取它,然后对该问题进行其余的处理。

因此,在您的服务器端脚本上,您必须将循环更改为简单地迭代 20 次(因为那是提交了多少问题)。这是您进行所有必要更改后两个文件的外观:

问题.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 文档以了解如何使用它,然后更改所有数据库内容以在您的代码中使用它。

祝你好运。

于 2014-10-01T07:13:17.867 回答