<?php
/**
* Fix Enrollments Date Column
*
* This script fixes the enrollments table by adding the missing enrollment_date column.
*
* Run this script if you encounter the error:
* "Unknown column 'e.enrollment_date' in 'order clause'"
*/
// Include database configuration
require_once '../../admin/database/db_config.php';
// Display header
echo "<!DOCTYPE html>
<html>
<head>
<title>Database Maintenance - Fix Enrollments Date Column</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; color: #333; }
h1 { color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; }
h2 { color: #3498db; margin-top: 20px; }
.container { max-width: 800px; margin: 0 auto; background: #f9f9f9; padding: 20px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
.success { color: #27ae60; background-color: #d5f5e3; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
.error { color: #c0392b; background-color: #f8d7da; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
.info { color: #2980b9; background-color: #d6eaf8; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
.warning { color: #f39c12; background-color: #fef9e7; padding: 10px; border-radius: 4px; margin-bottom: 10px; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
table, th, td { border: 1px solid #ddd; }
th, td { padding: 12px; text-align: left; }
th { background-color: #f2f2f2; }
a { color: #3498db; text-decoration: none; }
a:hover { text-decoration: underline; }
.btn { display: inline-block; background: #3498db; color: white; padding: 8px 16px; border-radius: 4px; text-decoration: none; margin-top: 15px; }
.btn:hover { background: #2980b9; text-decoration: none; }
pre { background: #f8f8f8; padding: 10px; border-radius: 4px; overflow-x: auto; }
code { font-family: monospace; font-size: 14px; }
</style>
</head>
<body>
<div class='container'>
<h1>Fix Enrollments Date Column</h1>
<p>This script checks and adds the missing enrollment_date column to the enrollments table.</p>";
try {
// Check if enrollments table exists
$table_exists = $conn->query("SHOW TABLES LIKE 'enrollments'");
if ($table_exists->num_rows == 0) {
echo "<div class='error'>Enrollments table does not exist. Please check your database setup.</div>";
} else {
echo "<div class='info'>Enrollments table exists. Checking columns...</div>";
// Check if enrollment_date column exists
$date_column_exists = $conn->query("SHOW COLUMNS FROM enrollments LIKE 'enrollment_date'");
$has_date_column = ($date_column_exists && $date_column_exists->num_rows > 0);
echo "<h2>Column Check Results:</h2>";
echo "<table>
<tr>
<th>Column Name</th>
<th>Status</th>
</tr>
<tr>
<td>enrollment_date</td>
<td>" . ($has_date_column ? "<span class='info'>Exists</span>" : "<span class='warning'>Missing</span>") . "</td>
</tr>
</table>";
// Add enrollment_date column if it doesn't exist
if (!$has_date_column) {
if ($conn->query("ALTER TABLE enrollments ADD COLUMN enrollment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP")) {
echo "<div class='success'>Successfully added 'enrollment_date' column to enrollments table.</div>";
// Update all existing enrollments with the current date
if ($conn->query("UPDATE enrollments SET enrollment_date = CURRENT_TIMESTAMP WHERE enrollment_date IS NULL")) {
echo "<div class='success'>Updated all existing enrollment records with current date.</div>";
} else {
echo "<div class='warning'>Added column but couldn't update existing records: " . $conn->error . "</div>";
}
} else {
echo "<div class='error'>Failed to add enrollment_date column: " . $conn->error . "</div>";
}
} else {
echo "<div class='info'>The enrollment_date column already exists. No action needed.</div>";
}
// Display SQL for future reference
echo "<h2>Useful SQL Commands:</h2>";
echo "<p>Here are the SQL commands used to fix the enrollment_date column:</p>";
echo "<pre><code>-- Add enrollment_date column if it doesn't exist
ALTER TABLE enrollments ADD COLUMN enrollment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
-- Update existing records with current date
UPDATE enrollments SET enrollment_date = CURRENT_TIMESTAMP WHERE enrollment_date IS NULL;</code></pre>";
// Check if the issue is fixed
$test_query = "SELECT e.*, u.first_name, u.last_name, c.title
FROM enrollments e
JOIN users u ON e.user_id = u.id
JOIN courses c ON e.course_id = c.id
ORDER BY e.enrollment_date DESC LIMIT 1";
// Try running the query that was causing the error
if ($conn->query($test_query)) {
echo "<div class='success'>✅ Test query ran successfully! The issue should be fixed.</div>";
} else {
echo "<div class='error'>❌ Test query still fails: " . $conn->error . " You may need additional fixes.</div>";
}
}
echo "<div class='links'>
<a href='../../index.php' class='btn'>Return to Homepage</a>
<a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
<a href='../index.php' class='btn'>Debug Tools</a>
</div>";
} catch (Exception $e) {
echo "<div class='error'>Error: " . $e->getMessage() . "</div>";
echo "<div class='links'>
<a href='../../index.php' class='btn'>Return to Homepage</a>
<a href='../../admin/index.php' class='btn'>Admin Dashboard</a>
<a href='../index.php' class='btn'>Debug Tools</a>
</div>";
}
echo "</div></body></html>";
?>