Maliha Khan Maliha Khan - 1 year ago 104
MySQL Question

passing the session variable

I have 3 files in my program, I am working on a quiz system.

quiz.php (first file)

In this file, I am fetching the categories from database

<div class="form-group">
<select class="form-control" id="" name="cat">
<label for="sel1">Select category</label>
<?php while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){

echo" <option value='".$row['cat_id']."'>".$row['cat_name']."</option>";
<center><button type="submit" class="btn btn-success" style="margin-left:-70px" >Start Quiz</button></center>

ques_show.php (second file)
this file displays the questions from database, based on category chosen.
I am saving the category in session variable

$cat=$_POST['cat']; // to get the category
$_SESSION['cat']=$cat; // saving category
//..... code

answer.php (3rd file)
This file displays the result

$cat_id=$_SESSION['cat']; // error here it says undefined
$response="SELECT * FROM questions WHERE cat_id='$cat_id'";

} else if($_POST[$row['q_id']]=="no_attempt"){
} else {


session variable
is used to check the saved answer from database where
cat = $cat_id

everything is working but error says in answer.php file undefined cat

What I am doing wrong

Answer Source

Things that might be wrong is, check if you initial session on page top. Other thing check if form post is working

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download