prepare(" SELECT q.* FROM practice_questions q WHERE q.track_id = ? AND q.id NOT IN ( SELECT question_id FROM practice_attempts WHERE student_name = ? AND is_correct = 1 ) ORDER BY q.id ASC LIMIT 1 "); $stmt->execute([$track_id, $student_name]); $question = $stmt->fetch(PDO::FETCH_ASSOC); if (!$question) { header("Location: track_complete.php?track_id=".$track_id); exit(); } $message = ""; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $selected = $_POST['answer']; $stmt = $pdo->prepare(" SELECT * FROM practice_attempts WHERE student_name = ? AND question_id = ? "); $stmt->execute([$student_name, $question['id']]); $attempt = $stmt->fetch(PDO::FETCH_ASSOC); $attempt_count = $attempt ? $attempt['attempt_count'] + 1 : 1; $is_correct = ($selected === $question['correct_option']) ? 1 : 0; if ($attempt) { // If already correct, don't downgrade it if ($attempt['is_correct'] == 1) { $is_correct = 1; } $stmt = $pdo->prepare(" UPDATE practice_attempts SET attempt_count = ?, is_correct = ? WHERE id = ? "); $stmt->execute([$attempt_count, $is_correct, $attempt['id']]); } else { $stmt = $pdo->prepare(" INSERT INTO practice_attempts (student_name, roll_number, track_id, question_id, attempt_count, is_correct) VALUES (?, ?, ?, ?, ?, ?) "); $stmt->execute([$student_name, $roll_number, $track_id, $question['id'], $attempt_count, $is_correct]); } if ($is_correct) { $message = "
Well done! Let’s move to the next challenge.
"; } else { $message = ($attempt_count == 1) ? "
{$question['hint_1']}
" : "
{$question['hint_2']}
"; } } ?>

Python Basics – Practice Challenge