<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "popularcomputer";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "<h2>Checking Exam Questions Data</h2>";
// Check if exam_questions table has data
$queryCount = "SELECT COUNT(*) as total FROM exam_questions";
$result = $conn->query($queryCount);
$row = $result->fetch_assoc();
$totalQuestions = $row['total'];
echo "<p>Total questions in the database: <strong>" . $totalQuestions . "</strong></p>";
// Check questions per exam
$queryExams = "SELECT es.id, es.title, COUNT(eq.id) as question_count
FROM exam_schedules es
LEFT JOIN exam_questions eq ON es.id = eq.exam_id
GROUP BY es.id
ORDER BY es.id";
$result = $conn->query($queryExams);
echo "<h3>Questions per Exam:</h3>";
echo "<table border='1'>";
echo "<tr><th>Exam ID</th><th>Exam Title</th><th>Question Count</th></tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['question_count'] . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='3'>No exams found</td></tr>";
}
echo "</table>";
// Check sample exam questions if they exist
if ($totalQuestions > 0) {
$querySample = "SELECT * FROM exam_questions LIMIT 5";
$result = $conn->query($querySample);
echo "<h3>Sample Questions:</h3>";
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Exam ID</th><th>Question Text</th><th>Question Type</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['exam_id'] . "</td>";
echo "<td>" . htmlspecialchars($row['question_text']) . "</td>";
echo "<td>" . $row['question_type'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
// Add sample questions if needed
echo "<h3>Add Sample Questions:</h3>";
echo "<form method='post'>";
echo "<p>If you want to add sample questions to an exam, select the exam:</p>";
$queryExamList = "SELECT id, title FROM exam_schedules ORDER BY id";
$result = $conn->query($queryExamList);
echo "<select name='exam_id'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['id'] . "'>" . $row['title'] . " (ID: " . $row['id'] . ")</option>";
}
echo "</select>";
echo "<input type='submit' name='add_questions' value='Add Sample Questions'>";
echo "</form>";
// Process form submission
if (isset($_POST['add_questions'])) {
$examId = intval($_POST['exam_id']);
// Add 5 sample questions
for ($i = 1; $i <= 5; $i++) {
$question = "Sample Question $i for Exam $examId";
$option1 = "Option 1 for question $i";
$option2 = "Option 2 for question $i";
$option3 = "Option 3 for question $i";
$option4 = "Option 4 for question $i";
$correctOption = rand(1, 4);
$stmt = $conn->prepare("INSERT INTO exam_questions (exam_id, question_text, question_type, option1, option2, option3, option4, correct_option, mark, question_order)
VALUES (?, ?, 'mcq', ?, ?, ?, ?, ?, 1, ?)");
$stmt->bind_param("isssssii", $examId, $question, $option1, $option2, $option3, $option4, $correctOption, $i);
$stmt->execute();
}
echo "<p style='color:green'>5 sample questions have been added to Exam ID $examId.</p>";
echo "<p><a href='check_exam_questions.php'>Refresh to see updated question count</a></p>";
}
// Close connection
$conn->close();
?>