Maliha Khan Maliha Khan - 1 month ago 7
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>";
}?>
</select><br>
<center><button type="submit" class="btn btn-success" style="margin-left:-70px" >Start Quiz</button></center>
</div>
</form>


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'";
$sql=mysqli_query($con,$response);


while($row=mysqli_fetch_assoc($sql)){
if($row['ans']==$_POST[$row['q_id']]){
$right_answer++;
} else if($_POST[$row['q_id']]=="no_attempt"){
$unanswered++;
} else {
$wrong_answer++;
}

}


session variable
$cat_id
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

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

Comments