<?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>Adding attempt_date column to student_exams table</h2>";
// Add attempt_date column
$addColumn = "ALTER TABLE student_exams
ADD COLUMN attempt_date DATETIME DEFAULT CURRENT_TIMESTAMP AFTER attempt_number";
if ($conn->query($addColumn) === TRUE) {
echo "<p>Successfully added attempt_date column to student_exams table.</p>";
// Update existing records to set attempt_date to created_at
$updateRecords = "UPDATE student_exams SET attempt_date = created_at";
if ($conn->query($updateRecords) === TRUE) {
echo "<p>Updated existing records with attempt_date values.</p>";
} else {
echo "<p>Error updating records: " . $conn->error . "</p>";
}
} else {
echo "<p>Error adding column: " . $conn->error . "</p>";
}
// Close connection
$conn->close();
echo "<a href='student/exam-results.php'>Go to Exam Results</a>";
?>