<?php
// Include database configuration
require_once '../admin/database/db_config.php';
// Enable error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
echo '<html><head><title>Database Structure Debug</title>';
echo '<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1, h2, h3 { color: #333; }
table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:nth-child(even) { background-color: #f9f9f9; }
.success { color: green; background-color: #dff0d8; padding: 10px; border-radius: 5px; margin: 10px 0; }
.error { color: red; background-color: #f2dede; padding: 10px; border-radius: 5px; margin: 10px 0; }
.info { color: #31708f; background-color: #d9edf7; padding: 10px; border-radius: 5px; margin: 10px 0; }
</style>';
echo '</head><body>';
echo "<h1>Database Structure Debug</h1>";
// Check courses table structure
echo "<h2>Courses Table Structure</h2>";
// Define expected columns
$expected_columns = [
'id' => 'INT(11) AUTO_INCREMENT PRIMARY KEY',
'title' => 'VARCHAR(255) NOT NULL',
'description' => 'TEXT',
'category' => 'INT(11)',
'instructor_id' => 'INT(11)',
'price' => 'DECIMAL(10,2) DEFAULT 0.00',
'duration' => 'VARCHAR(50) DEFAULT NULL',
'level' => 'VARCHAR(20) DEFAULT "beginner"',
'status' => 'VARCHAR(20) DEFAULT "active"',
'tags' => 'VARCHAR(255) DEFAULT NULL',
'is_featured' => 'TINYINT(1) DEFAULT 0',
'image' => 'VARCHAR(255) DEFAULT "assets/img/courses/default.jpg"',
'created_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
'updated_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
];
// Get existing columns
$existing_columns = [];
$result = $conn->query("SHOW COLUMNS FROM courses");
if ($result) {
echo "<table><tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['Field'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['Null'] . "</td>";
echo "<td>" . $row['Key'] . "</td>";
echo "<td>" . ($row['Default'] !== NULL ? $row['Default'] : "NULL") . "</td>";
echo "<td>" . $row['Extra'] . "</td>";
echo "</tr>";
$existing_columns[$row['Field']] = true;
}
echo "</table>";
} else {
echo "<div class='error'>Error fetching courses table structure: " . $conn->error . "</div>";
}
// Check for missing columns and add them
echo "<h2>Adding Missing Columns</h2>";
// Function to get the preceding column name
function getPrecedingColumn($columnName, $existingColumns, $expectedColumns) {
$columnKeys = array_keys($expectedColumns);
$currentIndex = array_search($columnName, $columnKeys);
if ($currentIndex > 0) {
for ($i = $currentIndex - 1; $i >= 0; $i--) {
if (isset($existingColumns[$columnKeys[$i]])) {
return $columnKeys[$i];
}
}
}
return null; // No preceding column found
}
// Add missing columns
$missingColumns = [];
foreach (array_keys($expected_columns) as $column) {
if (!isset($existing_columns[$column]) && $column != 'id') { // Skip primary key
$missingColumns[] = $column;
}
}
if (empty($missingColumns)) {
echo "<div class='success'>All required columns exist in the courses table.</div>";
} else {
echo "<div class='info'>The following columns need to be added: " . implode(', ', $missingColumns) . "</div>";
foreach ($missingColumns as $column) {
$preceding = getPrecedingColumn($column, $existing_columns, $expected_columns);
$after = $preceding ? " AFTER $preceding" : "";
// Extract the column definition
$definition = "";
switch ($column) {
case 'title':
$definition = "VARCHAR(255) NOT NULL";
break;
case 'description':
$definition = "TEXT";
break;
case 'category':
$definition = "INT(11) DEFAULT NULL";
break;
case 'instructor_id':
$definition = "INT(11) DEFAULT NULL";
break;
case 'price':
$definition = "DECIMAL(10,2) DEFAULT 0.00";
break;
case 'duration':
$definition = "VARCHAR(50) DEFAULT NULL";
break;
case 'level':
$definition = "VARCHAR(20) DEFAULT 'beginner'";
break;
case 'status':
$definition = "VARCHAR(20) DEFAULT 'active'";
break;
case 'tags':
$definition = "VARCHAR(255) DEFAULT NULL";
break;
case 'is_featured':
$definition = "TINYINT(1) DEFAULT 0";
break;
case 'image':
$definition = "VARCHAR(255) DEFAULT 'assets/img/courses/default.jpg'";
break;
case 'created_at':
$definition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP";
break;
case 'updated_at':
$definition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
break;
default:
$definition = "";
}
if ($definition) {
$sql = "ALTER TABLE courses ADD COLUMN $column $definition$after";
echo "<div>Executing: $sql</div>";
$result = $conn->query($sql);
if ($result) {
echo "<div class='success'>Successfully added $column column to courses table.</div>";
$existing_columns[$column] = true;
} else {
echo "<div class='error'>Failed to add $column column: " . $conn->error . "</div>";
}
}
}
}
// Check if course_categories table exists
echo "<h2>Course Categories Junction Table</h2>";
$result = $conn->query("SHOW TABLES LIKE 'course_categories'");
if ($result->num_rows > 0) {
echo "<div class='success'>course_categories table exists.</div>";
// Show structure
$result = $conn->query("SHOW COLUMNS FROM course_categories");
if ($result) {
echo "<table><tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['Field'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['Null'] . "</td>";
echo "<td>" . $row['Key'] . "</td>";
echo "<td>" . ($row['Default'] !== NULL ? $row['Default'] : "NULL") . "</td>";
echo "<td>" . $row['Extra'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
} else {
echo "<div class='info'>course_categories table does not exist. Creating it now...</div>";
// Create the course_categories junction table
$createTableSQL = "CREATE TABLE course_categories (
id INT(11) NOT NULL AUTO_INCREMENT,
course_id INT(11) NOT NULL,
category_id INT(11) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY unique_course_category (course_id, category_id),
KEY idx_course_id (course_id),
KEY idx_category_id (category_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
if ($conn->query($createTableSQL)) {
echo "<div class='success'>Successfully created course_categories table.</div>";
} else {
echo "<div class='error'>Failed to create course_categories table: " . $conn->error . "</div>";
}
}
echo "<h2>Next Steps</h2>";
echo "<p>Now you can return to the <a href='courses.php'>courses management page</a>.</p>";
echo "</body></html>";
?>