SQL查询以获取测验答案 [英] SQL query for getting answers for a quiz
问题描述
这是我的数据库设计:
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制作测验系统.我目前正在设计测验界面,我正在尝试显示特定问题的所有选择.我的问题是如何选择module_id
= 2和question_id
= 3的所有choices
?这是我的php代码:
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>
我遇到此错误
解析错误:语法错误,意外的''(T_ENCAPSED_AND_WHITESPACE), 期望'-'或标识符(T_STRING)或变量(T_VARIABLE)或 第11行的C:\ xampp \ htdocs \ Project \ quiz.php中的数字(T_NUM_STRING)
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
推荐答案
这些错误是由于语法问题或单引号或双引号的位置错误而引起的.
These error comes because of syntax problem or misplace position of single or double quotes.
您在代码中看到,您在div启动前两次关闭了php标签.
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.
不加入
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
这篇关于SQL查询以获取测验答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!