从MySQL数据库中选择随机问题; “正确答案"弄乱 [英] Selecting random questions from MySQL database; "correct answer" messed up

查看:61
本文介绍了从MySQL数据库中选择随机问题; “正确答案"弄乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP和MYSQL构建一个简单的测验程序.

I am building a simple quiz program, using PHP and MYSQL.

测验旨在一次显示一个问题;问题是多项选择题(每个问题有4个可能的答案)

The quiz is designed to display one question at a time; the questions are multiple-choice (each question has 4 possible answers)

如果玩家选择了正确的答案,则继续进行下一个问题; 如果他选错了,测验就结束了.

If the player picks the correct one, he proceeds to the next question; if he picks the wrong one, the quiz comes to an end.

首先,我将测验设计如下:

At first, I designed the quiz as follows :

(1)创建了一个数据库表,其中包含1500个问题.该表包括以下列:

(1) created a database table, which contains 1500 questions. The table has the following columns :

ID (primary key)
QUESTION (the question itself)
A1 (first answer)
A2 (second answer)
A3 (third answer)
A4 (fourth answer)
CORRECT  (the correct answer --- which is one of the above A1 to A4)

(2)然后,我的PHP代码被设计为以SEQUENTIAL顺序(使用ID作为参考)一个接一个地选择问题.

(2) Then, my PHP code was designed to pick questions, one by one, in SEQUENTIAL order (using the ID as reference).

因此,当用户开始玩游戏时,他从问题1开始,然后是问题2,依此类推,等等.

So, when the user starts playing, he begins with Question 1, then Question 2, etc, etc.

(3)为了使它更有趣,当用户在我的测验网站上注册为玩家时,我在数据库中添加了额外的列(begin_id),其默认值为"1".每次用户回答问题时,该列都会使用该问题的"ID"进行更新.意思是,它记录了用户回答的最后一个问题(无论是错误还是正确).这样一来:用户下次登录并进行测验时,他不会从Question-1开始.相反,他从列表中的下一个问题开始. (意味着,用户永远不会两次看到相同的问题!)

(3) To make it more interesting, I added an extra column to the database (begin_id), which has a default value of "1", when a user registers as a player on my quiz-website. Each time the user answers a question, this column is updated with that question's "ID". Meaning, it records the LAST question the user answered (whether wrongly or correctly). So that : the next time the user logs on, and plays the quiz, he does not begin from Question-1. Instead, he begins from the NEXT question in the list. (Meaning, the user never sees the same question twice!)

这是代码:

// Query database
$get_question = "SELECT * from questions_table where id = $begin_id";
$result_get_question = mysqli_query($conn, $get_question);
$row_get_question = mysqli_fetch_array($result_get_question);

// Assign database response to variables
$question = $row_get_question['question'];
$a1 = $row_get_question['a1'];
$a2 = $row_get_question['a2'];
$a3 = $row_get_question['a3'];
$a4 = $row_get_question['a4'];
$correct = $row_get_question['correct'];

// Check user answer
if (isset($_POST['submit'])) {   
    $selected_radio = $_POST['response'];

    if ($selected_radio == $correct)
        echo "THAT ANSWER IS CORRECT";
    else
        echo "THAT ANSWER IS WRONG!";
}

而且,一切正常!

现在,我决定使其更加高效";不再按顺序选择问题,我决定只修改PHP-SELECT语句即可随机选择问题.

Now, I decided to make it even more "efficient"; instead of selecting questions sequentially, I decided to simply modify my PHP-SELECT statement, to select questions RANDOMLY.

这很容易,尤其是因为有如此多的在线教程.

This was easy, especially as there are so many online tutorials on how this is done.

这是我使用的代码(最简单的代码):

Here is the code I used (the simplest one) :

SELECT * from questions_table order by rand() limit 1;

就选择随机问题而言,这是可行的.但是,出了点问题:无论用户选择什么答案,它始终是错误答案

This works, as far as selecting random questions goes. However, something is wrong : no matter what answer the user selects, it is always the WRONG answer!!

由于某种原因,从数据库中选择随机问题已经弄乱了我的PHP代码中的"正确答案/错误答案"逻辑.

For some reason, selecting random questions from the database has messed up the "Correct answer / Wrong answer" logic in my PHP code.

不知道怎么了.

更新

   $get_question = "SELECT * from questions_table where id = $begin_id";

   $result_get_question = mysqli_query($conn,$get_question);

   $row_get_question = mysqli_fetch_array($result_get_question);  

   $question_id = $row_get_question['question_id'];

   $question = $row_get_question['question'];

   $a1 = $row_get_question['a1'];

   $a2 = $row_get_question['a2'];

   $a3 = $row_get_question['a3'];

   $a4 = $row_get_question['a4'];

   $correct = $row_get_question['correct'];

  <br>

  <form method ="post" action ="">

  <input type="radio" name="response" value="<?=$a1?>"><?=$a1?><br>

  <input type="radio" name="response" value="<?=$a2?>"><?=$a2?><br>

  <input type="radio" name="response" value="<?=$a3?>"><?=$a3?><br>

  <input type="radio" name="response" value="<?=$a4?>"><?=$a4?><br>
          <input type="hidden" name="question_id" value="<?= echo $question_id?>" 
    />        <br>

  <Input type = "submit" Name = "submit" Value = "Answer">        </form>

推荐答案

当您向用户提问时,会从数据库中随机选择一个问题.

When you ask your question to the user, a question is randomly selected from the database.

然后,用户提交您的表单,随机选择另一个问题,这就是您用来检查答案的问题,而不是您向用户提出的问题.

Then, the user submits your form, another question is randomly selected, and that's the question you are using to check the answer, instead of the question you asked to the user.

您需要在表单中添加包含问题ID的隐藏输入

You need to add an hidden input in your form, that contains the question id

<input type="hidden" name="question_id" value="<?php echo $question_id ?>" />

然后,当您检查答案时,请确保从数据库中获取正确的问题

And then when you check the answer, be sure to fetch the right question from the database

代码看起来像这样

<?php

// Check user answer for previous question
if (isset($_POST['submit'])) {   
    // Retrieve the id of the question you asked
    $previous_question_id = (int) $_POST['question_id']; // cast to integer to prevent sql injection.

    // Query database
    $get_question = "SELECT * from questions_table where id = $previous_question_id";
    $result_get_question = mysqli_query($conn, $get_question);
    $row_get_question = mysqli_fetch_array($result_get_question);

    // Assign database response to variables
    $correct = $row_get_question['correct'];
    $selected_radio = $_POST['response'];

    if ($selected_radio == $correct)
        echo "THAT ANSWER IS CORRECT";
    else
        echo "THAT ANSWER IS WRONG!";
}


// Load new question to ask to the user
$get_question = "SELECT * from questions_table order by rand() limit 1";
$result_get_question = mysqli_query($conn,$get_question);
$row_get_question = mysqli_fetch_array($result_get_question);  

// assign thing we want to print in the template to variable
$question_id = $row_get_question['question_id'];
$question = $row_get_question['question'];
$a1 = $row_get_question['a1'];
$a2 = $row_get_question['a2'];
$a3 = $row_get_question['a3'];
$a4 = $row_get_question['a4'];

?>

<PASTE YOUR TEMPLATE HERE>

这篇关于从MySQL数据库中选择随机问题; “正确答案"弄乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆