prepare("SELECT title FROM skills WHERE id = ?"); $stmt->execute([$skill_id]); $skill_title = $stmt->fetchColumn(); if (!$skill_title) { die('Skill not found!'); } // Fetch quiz questions $questions_stmt = $db->prepare("SELECT * FROM quizzes WHERE skill_id = ?"); $questions_stmt->execute([$skill_id]); $questions = $questions_stmt->fetchAll(PDO::FETCH_ASSOC); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $score = 0; $total_questions = count($questions); foreach ($questions as $question) { $question_id = $question['id']; $user_answer = $_POST['question_' . $question_id] ?? ''; if ($user_answer === $question['correct_option']) { $score++; } } $percentage_score = ($total_questions > 0) ? round(($score / $total_questions) * 100) : 0; // Save quiz attempt $insert_stmt = $db->prepare("INSERT INTO quiz_attempts (user_id, skill_id, score) VALUES (?, ?, ?)"); $insert_stmt->execute([$user_id, $skill_id, $percentage_score]); header("Location: learn.php?skill_id=$skill_id"); exit; } ?> Quiz: <?php echo htmlspecialchars($skill_title); ?>
Back to Learning

Quiz:

No quiz questions available for this skill yet.
$question): ?>
Question