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
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
with inner join