<?php
/**
* Fix Enrollments Table
*
* This script fixes the enrollments table structure by:
* 1. Checking if student_id or user_id exists
* 2. Adding or renaming the column as needed
*
* Run this script if you encounter the error:
* "Unknown column 'e.student_id' in 'on clause'"
*/
// Include database configuration
require_once '../../admin/database/db_config.php';
// Display header
echo "<!DOCTYPE html>
<html>
<head>
<title>Database Maintenance - Fix Enrollments Table</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 Table</h1>
<p>This script checks and fixes the enrollments table structure to ensure proper references to the users 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 student_id column exists
$student_id_exists = $conn->query("SHOW COLUMNS FROM enrollments LIKE 'student_id'");
$has_student_id = ($student_id_exists && $student_id_exists->num_rows > 0);
// Check if user_id column exists
$user_id_exists = $conn->query("SHOW COLUMNS FROM enrollments LIKE 'user_id'");
$has_user_id = ($user_id_exists && $user_id_exists->num_rows > 0);
echo "<h2>Column Check Results:</h2>";
echo "<table>
<tr>
<th>Column Name</th>
<th>Status</th>
</tr>
<tr>
<td>student_id</td>
<td>" . ($has_student_id ? "<span class='info'>Exists</span>" : "<span class='warning'>Missing</span>") . "</td>
</tr>
<tr>
<td>user_id</td>
<td>" . ($has_user_id ? "<span class='info'>Exists</span>" : "<span class='warning'>Missing</span>") . "</td>
</tr>
</table>";
// Fix based on what exists
if ($has_student_id && !$has_user_id) {
// Rename student_id to user_id
if ($conn->query("ALTER TABLE enrollments CHANGE student_id user_id INT NOT NULL")) {
echo "<div class='success'>Successfully renamed 'student_id' column to 'user_id'.</div>";
} else {
echo "<div class='error'>Failed to rename column: " . $conn->error . "</div>";
}
} else if (!$has_student_id && !$has_user_id) {
// Both columns missing, add user_id
if ($conn->query("ALTER TABLE enrollments ADD COLUMN user_id INT NOT NULL AFTER id")) {
echo "<div class='success'>Successfully added 'user_id' column to enrollments table.</div>";
} else {
echo "<div class='error'>Failed to add column: " . $conn->error . "</div>";
}
} else if (!$has_student_id && $has_user_id) {
echo "<div class='info'>Table already has 'user_id' column. No action needed.</div>";
} else {
// Both columns exist (unlikely)
echo "<div class='warning'>Both 'student_id' and 'user_id' columns exist. Please check your database structure manually.</div>";
}
// Check payments table as well
echo "<h2>Checking Payments Table:</h2>";
$payments_exists = $conn->query("SHOW TABLES LIKE 'payments'");
if ($payments_exists->num_rows > 0) {
// Check if student_id column exists in payments
$student_id_payments = $conn->query("SHOW COLUMNS FROM payments LIKE 'student_id'");
$has_student_id_payments = ($student_id_payments && $student_id_payments->num_rows > 0);
// Check if user_id column exists in payments
$user_id_payments = $conn->query("SHOW COLUMNS FROM payments LIKE 'user_id'");
$has_user_id_payments = ($user_id_payments && $user_id_payments->num_rows > 0);
echo "<table>
<tr>
<th>Column Name</th>
<th>Status</th>
</tr>
<tr>
<td>student_id</td>
<td>" . ($has_student_id_payments ? "<span class='info'>Exists</span>" : "<span class='warning'>Missing</span>") . "</td>
</tr>
<tr>
<td>user_id</td>
<td>" . ($has_user_id_payments ? "<span class='info'>Exists</span>" : "<span class='warning'>Missing</span>") . "</td>
</tr>
</table>";
// Fix payments table if needed
if ($has_student_id_payments && !$has_user_id_payments) {
// Rename student_id to user_id in payments
if ($conn->query("ALTER TABLE payments CHANGE student_id user_id INT NOT NULL")) {
echo "<div class='success'>Successfully renamed 'student_id' to 'user_id' in payments table.</div>";
} else {
echo "<div class='error'>Failed to rename column in payments table: " . $conn->error . "</div>";
}
} else if (!$has_student_id_payments && !$has_user_id_payments) {
// Both columns missing, add user_id to payments
if ($conn->query("ALTER TABLE payments ADD COLUMN user_id INT NOT NULL AFTER id")) {
echo "<div class='success'>Successfully added 'user_id' column to payments table.</div>";
} else {
echo "<div class='error'>Failed to add column to payments table: " . $conn->error . "</div>";
}
} else if (!$has_student_id_payments && $has_user_id_payments) {
echo "<div class='info'>Payments table already has 'user_id' column. No action needed.</div>";
} else {
// Both columns exist (unlikely)
echo "<div class='warning'>Both 'student_id' and 'user_id' columns exist in payments table. Please check your database structure manually.</div>";
}
} else {
echo "<div class='info'>Payments table doesn't exist. No action taken.</div>";
}
// Display SQL to update index.php query
echo "<h2>Required Code Changes:</h2>";
echo "<p>After fixing the database tables, you need to update the queries in admin/index.php:</p>";
echo "<pre><code>// For enrollments query:
\$enrollments_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 5\";
// For payments query (if exists):
\$payments_query = \"SELECT p.*, CONCAT(u.first_name, ' ', u.last_name) as user_name
FROM payments p
JOIN users u ON p.user_id = u.id
ORDER BY p.payment_date DESC LIMIT 5\";</code></pre>";
}
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>";
?>