从mysql数据库中随机抽取多项选择。在成绩页面上使用正确答案

时间:2016-02-02 22:58:58

标签: php mysql

我已经生成了一个多项选择问题页面,在我的数据库中从我的qbanktable中随机提问。正确回答这个问题也在qbanktable中。这是question.php页面的代码

<Link to="/status" activeClassName="active">STATUS</Link>

现在按完提交按钮后,它会指示我到grade.php,它将分析CorrectAnswer:

<form action="grades.php" method="post" id="quiz">
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT Question, AnswerA, AnswerB, AnswerC, AnswerD, AnswerE, CorrectAnswer FROM qbanktable ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);


?>

                  <h3>
                  <?php

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["Question"];
    }
}
       ?>                  

                 </h3>
                  <p>&nbsp;</p>
                  <div>
                        <input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
                        <label for="question-1-answers-A">A)&nbsp;     
                            <?php
                                $result = $conn->query($sql);
                                if ($result->num_rows > 0) {
                                    while($row = $result->fetch_assoc()) {
                                        echo $row["AnswerA"];
                                        }
                                    }
                                    ?>      
                        </label>
                  </div>

                    <div>
                        <input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
                        <label for="question-1-answers-B3">B)&nbsp;
                            <?php
                                $result = $conn->query($sql);
                                if ($result->num_rows > 0) {
                                    while($row = $result->fetch_assoc()) {
                                        echo $row["AnswerB"];
                                        }
                                    }
                                    ?>  
                        </label>
                    </div>

                    <div>
                        <input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
                        <label for="question-1-answers-C3">C)&nbsp;
                            <?php
                                $result = $conn->query($sql);
                                if ($result->num_rows > 0) {
                                    while($row = $result->fetch_assoc()) {
                                        echo $row["AnswerC"];
                                        }
                                    }
                                    ?>  
                        </label>
                    </div>

                    <div>
                        <input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
                        <label for="question-1-answers-D3">D)&nbsp;
                            <?php
                                $result = $conn->query($sql);
                                if ($result->num_rows > 0) {
                                    while($row = $result->fetch_assoc()) {
                                        echo $row["AnswerD"];
                                        }
                                    }
                                    ?>  
                        </label>
                    </div>
                     <div>
                        <input type="radio" name="question-1-answers" id="question-1-answers-D" value="E" />
                        <label for="question-1-answers-D3">E)&nbsp;
                            <?php
                                $result = $conn->query($sql);
                                if ($result->num_rows > 0) {
                                    while($row = $result->fetch_assoc()) {
                                        echo $row["AnswerE"];
                                        }
                                    }
                                    ?>  
                        </label>
                    </div>
                    <div>
                    <input type="hidden" name="question-1-correct_answer" id="question-1-correct-answer" value="$row["CorrectAnswer"]" >
                </div>
          </ol>

            <input type="submit" class="hvr-grow" value="SUBMIT" />
    </form>

我现在相信grade.php(上面的代码)我搞砸了一些简单的东西。我想知道将CorrectAnswer与用户答案相匹配的正确代码是什么?谢谢

2 个答案:

答案 0 :(得分:0)

多种方式:

  1. 在隐藏输入中发送问题ID并重新查询数据库以获得正确答案,这要求您编辑查询以获取问题ID

    <input type="hidden" name="question-id" id="question-id" value="$row["id"]" >
    
  2. 在grades.php中,您可以使用重新查询数据库来获取所有答案(尤其是正确答案)

        $questionID= $_POST['question-id'];
    

    第二个更容易但不太安全,所以先尝试第一个选项

    1. 您可以使用隐藏的输入类型发送它,但正如@khuderm已经评论过的那样,这是一个坏主意,因为技能很少的人可以在源代码中看到答案。我不认为有人会尝试它,但更安全然后抱歉。

      <input type="hidden" name="question-1-correct_answer" id="question-1-correct-answer" value="$row["CorrectAnswer"]" >
      
    2. grades.php中你需要这样的东西:

      $servername = "localhost";
      $username = "root";
      $password = "root";
      $dbname = "qbank";
      $questionID= $_POST['question-id'];
      $conn = new mysqli($servername, $username, $password, $dbname);
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      }
      
      $sql = "SELECT CorrectAnswer FROM qbanktable where id = $questionID";
      $result = $conn->query($sql);
      

答案 1 :(得分:0)

所以经过一些修改后,现在可行了。

1)我必须在php中包含隐藏输入的标签,如下所示:

<input type="hidden" name="question-id" id="question-id" value=     <?php
                                $result = $conn->query($sql);
                                if ($result->num_rows > 0) {
                                    while($row = $result->fetch_assoc()) {
                                        echo $row["id"];
                                        }
                                    }
                                    ?> >

现在它从我的表中获取id行并将其发送到grades.php

2)在grades.php中,代码最终用最后一页提供的问题id中的正确答案检查用户输入:

        <?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";
$questionID= $_POST['question-id']; 
$answer1 = $_POST['question-1-answers'];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT CorrectAnswer FROM qbanktable WHERE id = $questionID";
$result = $conn->query($sql);
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    $correct = $row["CorrectAnswer"];
                                        }
                                    }

            if ($answer1 == $correct) {

            echo "<img src=correct.svg";
            }
            else {
            echo "<img src=wrong.svg";
            }
?> 

现在它运作正常。 特别感谢@davejal