Could anyone please help me figure out the errors in my code.
Error 1: When the user clicks 'quiz result' the selected answer on the final question isn't brought through to the quiz_results.php
Error 2: When the user clicks the correct answer on any question it isn't added to the rightAnswers and thus their results display 'Correct answers: 0;'
quiz.php:
<?php
if(isset($_POST['checkQuiz'])) {
$a=$_POST['a'];
$quiz_id=$_SESSION['quiz_id'];
$index=$_SESSION['index'];
$resultQuery = mysqli_query($con,"SELECT `correctValue` FROM quiz_questions WHERE quiz_id = '$quiz_id' LIMIT 1 OFFSET $index");
$cor=0;
$incorrect=0;
while ($correct = mysqli_fetch_array($resultQuery))
{
if ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
if ($_POST['answer'] != $correct[0]) {
$_SESSION['wrongAnswers']+=1;
}
}
}
if(isset($_POST['checkResult'])) {
$a=$_POST['a'];
//quiz variables
$quiz_id=$_SESSION['quiz_id'];
$index=$_SESSION['index'];
//query to get correct answet for current question
$resultQuery = mysqli_query($con,"SELECT `correctValue` FROM quiz_questions WHERE quiz_id = '$quiz_id' LIMIT 1 OFFSET $index");
$cor=0;
$incorrect=0;
//while loop for correct answer
while ($correct = mysqli_fetch_array($resultQuery))
{
//if radio button check correct add 1 to right answers
if ($_POST['answer'] == $correct[0]) {
$_SESSION['rightAnswers']+=1;
}
// if not correct add 1 to wrong answers
if ($_POST['answer'] != $correct[0]) {}
$_SESSION['wrongAnswers']+=1;
}
}
if($a==$_SESSION['numberOfQuestions']){
header("Location: quiz_result.php");
}
}
//score 0 id $a not set
if(!isset($a)){
$a=0;
$_SESSION['rightAnswers']=0;
$_SESSION['wrongAnswers']=0;
}
$quiz_id = $_GET['quiz_id'];
$_SESSION['index']=$a;
$questionNumber = $a+1;
$_SESSION['quiz_id']=$quiz_id;
$numberOfQuestionsQuery = mysqli_query($con, "SELECT COUNT(quiz_id) AS numberOfQuestions FROM quiz_questions WHERE quiz_id ='$quiz_id'");
$numberOfQuestionsResult = mysqli_fetch_object($numberOfQuestionsQuery);
$_SESSION['numberOfQuestions']=$numberOfQuestionsResult->numberOfQuestions;
$numberQuizQuestions=$_SESSION['numberOfQuestions'];
$get_questions = mysqli_query($con,"SELECT * FROM `quiz_questions` WHERE `quiz_id` = '$quiz_id' LIMIT 1 OFFSET $a");
while ($result = mysqli_fetch_array($get_questions)) { ?>
<form id="question_no" ><div class="left"></div> <div class="right">No. of questions: <?php echo $numberQuizQuestions; ?> </div> </form> <br> <br><br>
<form method="post" action="" class="form complete">
<table>
<td>
<td width = "50" id="question"><?php echo $result['question'] . "<br>"; ?></td>
</td>
<tr height = "10"></tr>
<td id= "number" width = "20" class="number"><?php echo $questionNumber ?>)</td>
<td id = "possible_answers" height = "100"width = "700">
<input type="radio" name="answer" value="<?php echo $result['answerA'] ?>"> <?php echo $result['answerA']; ?> <br>
<input type="radio" name="answer" value="<?php echo $result['answerB'] ?>"> <?php echo $result['answerB']; ?> <br>
<input type="radio" name="answer" value="<?php echo $result['answerC'] ?>"> <?php echo $result['answerC']; ?> <br>
<input type="radio" name="answer" value="<?php echo $result['answerD'] ?>"> <?php echo $result['answerD']; ?> <br><br> </td>
</table>
<?php
$_SESSION['questionNumber']=$questionNumber;
}
$a=$a+1;
?>
<a href="interactive_training.php" id="button1">« Back to training</a>
<?php //next question if less than number of questions
if ($questionNumber<$_SESSION['numberOfQuestions']) {
?>
<input type="submit" name="checkQuiz" value="Next Question" id="button1">
<input type="hidden" value="<?php echo $a ?>" name="a">
<?php } ?>
<?php //check results = end of quiz
if ($questionNumber==$_SESSION['numberOfQuestions'])
{ ?>
<a type="submit" href = "quiz_result.php?session_id=<?php echo $_GET['session_id']; ?>" name="checkResult" id="button1"> Quiz Result </a>
<?php } ?>
quiz_results.php
<div class="result">Result</div> <hr> <br>
<?php
include("includes/database.php");
$currentUserID = $_SESSION['currentUserID'];
$session_id = $_GET['session_id'];
$correct = $_SESSION['rightAnswers'];
$questions = $_SESSION['questionNumber'];
$percent = $correct/$questions;
$percent_friendly = number_format( $percent * 100, 0 ) . '%';
$_SESSION['percent']=$percent_friendly;
?>
<form>
<div><input class="result" Value ="Quiz ID:- <?php echo $_SESSION['quiz_id'];?>" readonly> </div><br>
<div><input class="result" Value ="Questions Answered:- <?php echo $_SESSION['questionNumber'];?>" readonly> </div><br>
<div><input class="result" Value ="Correct Answers:- <?php echo $_SESSION['rightAnswers'];?>" readonly> </div><br>
<div><input class="result" Value ="Incorrect Answers:- <?php echo $_SESSION['wrongAnswers'];?>" readonly> </div><br>
<div><input class="result" Value ="Result Percentage:- <?php echo $_SESSION['percent'];?>" readonly> </div><br>
<hr>
Any help would be greatly apprecaited