I trying to display a quiz from a list of quizzes depending on the current topic the user has clicked (topics are called sessions). Sessions have exercises and on every exercise I want the option to go to the specific session quiz (A session has 5 exercises and 1 quiz). When the quiz
button is clicked, I'm trying to display the quiz title and author using the current $session_id
and linking that to the session_id
from the quiz_list
table in the database.
Nothing is displaying and I'm getting errors like:
Notice: Undefined index: session_id in C:\xampp\htdocs\project\quiz.php on line 8
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Project\quiz.php on line 13
exercise.php:
<?php
//build up array as iterate through while loop
$sessions_quiz_array = array();
$get_quiz = mysqli_query($con, "SELECT * FROM `quiz_list` WHERE `session_id` = $session_id");
//fetches result row as an associative array
while ($row_quizes = mysqli_fetch_assoc($get_quiz)) {
$sessions_quiz_array[] = $row_quizes;
}
//sessions_quiz_array is the associative array being looped
foreach ($sessions_quiz_array as $key => $value) {
$quiz_id = $value['id'];
$quiz_title = $value['quiz_title'];
$quiz_author = $value['quiz_author'];
?>
<a id='button' href="quiz.php?id=<?php echo $quiz_id; ?>"> Quiz</a>
<?php } ?>
quiz.php
<?php
session_start();
include("includes/database.php");
?>
<?php
//gets current
$session_id = $_GET['session_id'];
$get_quiz = "SELECT * FROM `quiz_list` WHERE `session_id` = $session_id";
$run_quiz = mysqli_query($con, $get_quiz);
$quiz_info = mysqli_fetch_assoc($run_quiz);
?>
<hr>
<div>
<h1><?php echo $quiz_info["id"]; ?> </h1>
<br>
<p><?php echo $quiz_info["quiz_title"]; ?></p>
<br>
<p><strong><?php echo $quiz_info["quiz_author"]; ?></strong></p>
</div>
<hr>
<a href="interactive_training.php" class="button previous">« Back to Training</a>
Any help would be greatly appreciated.
In you file exercise.php
your GET request is ID
not session_id
near quiz.php?id=
You need to make following change in your quiz.php
file.
Change from
$session_id = $_GET['session_id'];
To
$session_id = $_GET['id'];
Addiotionally you can add check before executing whole page like
if(!isset($session_id))
{
exit('Session ID is missing');
}
Your quiz.php becomes:-
replace this
$session_id = $_GET['session_id'];
with
$session_id = $_GET['id'];