【问题标题】:Selecting random questions from MySQL database; "correct answer" messed up从 MySQL 数据库中选择随机问题; “正确答案”搞砸了
【发布时间】:2015-11-02 02:12:32
【问题描述】:

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

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

如果玩家选择了正确的问题,他会继续下一个问题; 如果他选错了,测验就结束了。

一开始,我设计的测验如下:

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

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 代码设计为按顺序(使用 ID 作为参考)一一选择问题。

所以,当用户开始玩时,他从问题 1 开始,然后是问题 2,以此类推。

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

代码如下:

// 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 语句,以随机选择问题。

这很容易,尤其是有很多关于如何做到这一点的在线教程。

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

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

就选择随机问题而言,这很有效。但是,有些问题:无论用户选择什么答案,它始终是错误答案!!

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

不知道可能出了什么问题。

更新

   $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>

【问题讨论】:

  • 请将您的代码重构为可读。并且不要对问题使用粗体字。重要时使用blockquotes
  • 为什么不在 PHP 或 SQL 代码中为您的 ID 生成一个随机数?
  • @Klaar:不确定我是否理解。请问你能详细说明吗?谢谢
  • 您检查过变量包含的内容吗?即通过打印或回显变量。这应该让您更好地了解返回的内容。
  • 您使用的代码“(最简单的)”将 1)从 questions_table 中选择所有数据(所有列的所有行),2)按其引用的随机列对结果进行排序编号,然后 3) 修剪结果以仅保留第一行。那样不是很好;应该通过随机化 WHERE 子句中的 ID 来随机化选定的行,而不是通过随机列排序。

标签: php mysql database random


【解决方案1】:

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

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

您需要在表单中添加一个隐藏输入,其中包含问题 ID

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

然后当你检查答案时,一定要从数据库中获取正确的问题

代码如下所示

<?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>

【讨论】:

  • 很好看,没想到他在处理完表格后又去取数据了。对于没有经验的人,您应该期待任何事情。
  • @Eloims:感谢您的回复。我更新了我的原始帖子以显示我的表格。我不太确定如何在 SQL select 语句中实现“question_id”逻辑,因为它是随机选择的。
  • 您需要从 POST if 语句中查询数据库。例如if (isset($_POST['submit'])) { //query database for correct answer where id = $questionId }
  • @Eloims:非常感谢 Eloims。它工作得很好。但是,我有一个问题:这一行的目的是什么: ;另外,如果是“echo”语句,不应该以分号结尾吗?
  • 应该这样写:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 2011-11-16
  • 2022-11-13
  • 2018-11-16
相关资源
最近更新 更多