prepare("SELECT id, total_questions FROM manual_quizzes WHERE id = ? LIMIT 1"); $stmtQ->bind_param('i', $quiz_id); $stmtQ->execute(); $qres = $stmtQ->get_result(); if ($qres->num_rows === 0) { $stmtQ->close(); die('Quiz not found.'); } $quizRow = $qres->fetch_assoc(); $stmtQ->close(); $totalQuestions = (int)$quizRow['total_questions']; // Normalize answers to totalQuestions length and uppercase $normalized = []; for ($i = 0; $i < $totalQuestions; $i++) { $val = isset($answersArr[$i]) ? strtoupper(trim($answersArr[$i])) : ''; // accept only A/B/C/D or blank if (!in_array($val, ['', 'A', 'B', 'C', 'D'], true)) $val = ''; $normalized[] = $val; } // join as CSV string e.g. "A,B,,D,..." $answersStr = implode(',', $normalized); // Save to DB (manual_answers table) $createdBy = (int)$_SESSION['user_id']; $stmt = $conn->prepare(" INSERT INTO manual_answers (quiz_id, student_name, roll_no, answers, created_by_user_id) VALUES (?, ?, ?, ?, ?) "); if (!$stmt) { die('DB prepare failed: ' . $conn->error); } $stmt->bind_param('isssi', $quiz_id, $student_name, $roll_no, $answersStr, $createdBy); if ($stmt->execute()) { $insertId = $stmt->insert_id; $stmt->close(); // Redirect to result/profile page for this answer header('Location: manual_result_profile.php?answer_id=' . $insertId); exit; } else { $err = $stmt->error; $stmt->close(); die('Failed to save answers: ' . htmlspecialchars($err)); }