dongpi2503 2018-03-03 18:25
浏览 37
已采纳

用于获取测验答案的SQL查询[重复]

This question already has an answer here:

This is my database design:

module: 
module_id   
module_name

questions: 
question_id 
module_id 
question

choice: 
choice_id 
question_id 
choice 
is_correct

And these are samples for the database.

Module

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

Questions

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

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

I am currently making a quiz system in php. Im currently designing the quiz interface and I am trying to display all the choices for a specific question. My question is how would I select all the choices with module_id = 2 and question_id = 3?. This is my php code:

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

EDIT: I am getting this error

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\Project\quiz.php on line 11

</div>
  • 写回答

1条回答 默认 最新

  • dongxuxian1123 2018-03-03 19:08
    关注

    These error comes because of syntax problem or misplace position of single or double quotes.

    You see in your code, you have closed php tag two time before div start.

    I hope, It will help.

    You can modify the query and try with the new one.

    without join

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

    with inner join

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面