用于获取测验答案的SQL查询

时间:2018-03-03 18:25:05

标签: php mysql sql database mysqli

这是我的数据库设计:

module: 
module_id   
module_name

questions: 
question_id 
module_id 
question

choice: 
choice_id 
question_id 
choice 
is_correct

这些是数据库的样本。

模块

+-----------+-------------+
| module_id | module_name |
+-----------+-------------+
|  1        | Forces      |
|  2        | Magnetism   |
|  3        | Electricity |
|  4        | Gravitation |
+-----------+-------------+

问题

+------------+-----------+-----------------+
| question_id| module_id | question        |
+------------+-----------+-----------------+
|  1         | 1         | Define a Newton |
|  2         | 1         | Define Work Done|
|  3         | 2         | Define Magnetism|
|  4         | 2         | Define a Tesla  |
|  5         | 3         | Define Current  |
+------------+-----------+-----------------+

选择

+----------+-------------+-------------------+----------+
| choice_id| question_id | choices           |is_correct|
+----------+-------------+-------------------+----------+
|  1       | 1           | Answer 1          |  0       |
|  2       | 1           | Answer 2          |  0       |
|  3       | 1           | Answer 3          |  0       |
|  4       | 1           | Answer 4          |  1       |
|  5       | 2           | Answer 1          |  0       |
|  6       | 2           | Answer 2          |  0       |
|  7       | 2           | Answer 3          |  0       |
|  8       | 2           | Answer 4          |  1       |
|  9       | 3           | Answer 1          |  0       |
|  10      | 3           | Answer 2          |  0       |
|  11      | 3           | Answer 3          |  0       |
|  12      | 3           | Answer 4          |  1       |
+----------+-------------+-------------------+----------+

我目前正在使用php进行测验系统。我正在设计测验界面,我试图显示特定问题的所有选择。我的问题是如何选择choices = 2和module_id = 3?question_id的所有<?php require_once 'includes/config.php'; include 'main.php'; $numberQuestions = $_GET['numberQuestions']; $module = $_GET['module']; $sqlQuestion = "SELECT * FROM questions WHERE module_id = $module"; $questionResult = mysqli_query($connect, $sqlQuestion); $question = mysqli_fetch_assoc($questionResult); $sqlChoices = "SELECT * FROM choice AS C INNER JOIN questions AS Q ON C.question_id=Q.question_id WHERE Q.module_id = $module AND C.question_id = $question['question_id']"; $choicesResult = mysqli_query($connect, $sqlChoices); ?> ?> <div class="wrapper"> <div class="quizContainer"> <div class="current">Question 1 of 5</div> <p class="question"><?php echo $question['question'];?></p> <form method="POST" action="process.php"> <div class="choiceContainer"> <ul> <?php while($row = mysqli_fetch_assoc($choicesResult)): ?> <li><input name="choice" type="radio" value="<?php echo $row[choice_id];?>"/><?php echo $row['choice'];?></li> <?php endwhile; ?> </ul> </div> <button type="submit" name="submit">Submit</button> <input type="hidden" name="numberQuestions" value="<?php echo $numberQuestions; ?>"/> </form> </div> </div> 。这是我的PHP代码:

public function duplicateItinRow($itineraryID){
    $this->db->query("CREATE TEMPORARY TABLE temporary_itin_table AS SELECT * FROM itinerary_ref WHERE Itinerary_ID = $itineraryID;

    UPDATE temporary_itin_table SET Itinerary_ID=NULL;

    INSERT INTO itinerary_ref SELECT * FROM temporary_itin_table;

    DROP TEMPORARY TABLE temporary_itin_table");

    //Execute
    if($this->db->execute()){           
        return $this->db->lastInsertId();

    } else {
        return false;
    }


}

编辑:我收到此错误

  

解析错误:语法错误,意外&#39;&#39; (T_ENCAPSED_AND_WHITESPACE),   期待&#39; - &#39;或标识符(T_STRING)或变量(T_VARIABLE)或   第11行的C:\ xampp \ htdocs \ Project \ quiz.php中的编号(T_NUM_STRING)

1 个答案:

答案 0 :(得分:0)

这些错误是由于单引号或双引号的语法问题或错位。

您在代码中看到,在div启动之前已经关闭了两次php标记。

我希望,这会有所帮助。

您可以修改查询并尝试使用新查询。

不加入

Select ch.choices from Question as Q, Choice as ch where Q.module_id=2 and ch.question_id = 3
内部联接

Select choices from Question as Q INNER JOIN choice as ch on Q.question_id = ch.question_id where Q.module_id=2 and ch.question_id=3