connect_error) { die("Connection failed: " . $conn->connect_error); } // Fetch 5 random questions from the database $sql = "SELECT * FROM questions ORDER BY RAND() LIMIT 5"; $result = $conn->query($sql); $questions = []; while ($row = $result->fetch_assoc()) { $questions[] = $row; } // Store correct answers in session $correct_answers = []; foreach ($questions as $index => $question) { $correct_answers[] = $question['correct_option']; } $_SESSION['correct_answers'] = $correct_answers; // Initialize the score if it's not already in session if (!isset($_SESSION['score'])) { $_SESSION['score'] = 0; } if ($_SERVER['REQUEST_METHOD'] == 'POST') { $current_question_index = $_POST['current_question_index']; $answer = $_POST['answer']; // Check if the answer is correct and increment the score if (isset($_SESSION['correct_answers'][$current_question_index]) && $answer == $_SESSION['correct_answers'][$current_question_index]) { $_SESSION['score']++; // Increment score in session } // Move to the next question or finish if it's the last question $next_question_index = $current_question_index + 1; if ($next_question_index < count($questions)) { echo json_encode([ 'next_question_index' => $next_question_index, 'score' => $_SESSION['score'], 'next_question' => $questions[$next_question_index] ]); } else { // Redirect to the result page after the last question header("Location: result.php"); exit(); } exit; } ?> Quiz Application

Welcome to the Quiz!