用于获取测验答案的SQL查询[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于获取测验答案的SQL查询[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
这是我的数据库设计:
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代码:
<?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)或数字(T_NUM_STRING)在第11行的C: xampp htdocs Project quiz.php中
答案
这些错误是由于语法问题或单引号或双引号的错位。
您在代码中看到,在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
以上是关于用于获取测验答案的SQL查询[重复]的主要内容,如果未能解决你的问题,请参考以下文章
sql [SQL查询片段]用于在命令行或通过R和其他工具使用SQL的快速代码段#tags:sql,R,text processing,命令li