用PHP的3个选项显示问题

时间:2019-10-17 08:28:53

标签: php mysql sql

因此,我正在一个项目中,我有一个充满问题/选项的数据库。每个问题有3个选项(其中1个是正确的)。
在用户开始测验之前,他选择他想问多少个问题。我通过选择表单来执行此操作。然后,PHP将值从选择表单发送到测验页面。在SQL查询中使用的位置:

SELECT *
FROM quiz_question
WHERE quiz_id = 1
ORDER BY RAND()
LIMIT $number

$number代表选择表单的值
我可以显示问题,但不能显示所选问题的选项。
我的代码:

<div id="quiz">
  <?php
  $number = $_POST['number'];
  $sql = "SELECT *
    FROM quiz_question
    WHERE quiz_id = 1
    ORDER BY RAND()
    LIMIT $number";
  $result = mysqli_query($conn,$sql);
  if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
      echo "<p>";
      echo $row['question'];
      echo "</p>";
    }
  }
  ?>
</div>

我的数据库如下所示:

quiz

  • id(PK)
  • quiz_name(文本)

quiz_question

  • id(PK)
  • quiz_id(FK)
  • question(文本)

quiz_question_option

  • id(PK)
  • quiz_question_id(FK)
  • quiz_option(文本)
  • is_correct(枚举0、1)

编辑: 我尝试使用INNER JOIN,但是当我使用ORDER BY RAND()LIMIT时,我无法获得单个问题的全部3个选项。

1 个答案:

答案 0 :(得分:1)

只需搁置SQL注入问题(您应该使用Prepared语句),就可以使用辅助查询来获取测验的所有选项以进行渲染。

<div id="quiz">
  <?php

    // aggregate an array of options
    $option_results = mysqli_query($conn, "SELECT qo.* FROM quiz_question_option qo
        LEFT JOIN quiz_question q ON (qo.quiz_question_id = q.id)
        WHERE q.quiz_id = 1");
    $options = [];
    while ($option = mysqli_fetch_assoc($option_results)) {
        $options[$option['quiz_question_id']] = $option;
    }
    mysqli_free_result($option_results);

    $number = $_POST['number'];
    $sql = "SELECT * FROM quiz_question WHERE quiz_id = 1 ORDER BY RAND() LIMIT $number";
    $result = mysqli_query($conn,$sql);

    if (mysqli_num_rows($result) > 0)
    {
         while ($question = mysqli_fetch_assoc($result)) {
             echo "<p>";
             echo $question['question'];
             echo "</p>";
         }

         if (isset($options[$question['id']])) {
             echo "<ul>";
             foreach ($options[$question['id']] as $option) {
                 echo $option['quiz_option'];
             }
             echo "</ul>";
         }
    }
  ?>
</div>

现在,要转换为准备好的语句:

<div id="quiz">
  <?php
    $number = $_POST['number'] ?? 10000;
    $quiz_id = 1;

    // aggregate an array of all questions' options of a quiz
    $option_stmts = mysqli_prepare($conn, "SELECT qo.* FROM quiz_question_option qo
        LEFT JOIN quiz_question q ON (qo.quiz_question_id = q.id)
        WHERE q.quiz_id = ?");
    $option_stmts->bind_param('i', $quiz_id);
    $option_results = $option_stmts->execute();
    $options = [];
    while ($option = mysqli_fetch_assoc($option_results)) {
        $options[$option['quiz_question_id']] = $option;
    }
    mysqli_free_result($option_results);
    mysqli_stmt_close($option_stmts);

    // query for all the questions of a quiz
    $stmt = mysqli_prepare($conn, "SELECT * FROM quiz_question WHERE quiz_id = ? ORDER BY RAND() LIMIT ?");
    $stmt->bind_param('ii', $quiz_id, $number);
    $result = $stmt->execute();

    if (mysqli_num_rows($result) > 0)
    {
         while ($question = mysqli_fetch_assoc($result)) {
             echo "<p>";
             echo $question['question'];
             echo "</p>";
         }

         if (isset($options[$question['id']])) {
             echo "<ul>";
             foreach ($options[$question['id']] as $option) {
                 echo $option['quiz_option'];
             }
             echo "</ul>";
         }
    }
  ?>
</div>