<?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>Course Reviews Table 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; }
.actions { margin: 20px 0; }
</style>';
echo '</head><body>';
echo "<h1>Course Reviews Table Debug</h1>";
// Check if course_reviews table exists
$table_exists = $conn->query("SHOW TABLES LIKE 'course_reviews'");
$course_reviews_exists = ($table_exists && $table_exists->num_rows > 0);
if ($course_reviews_exists) {
echo "<div class='success'>The course_reviews table exists.</div>";
// Display current table structure
echo "<h2>Current Table Structure</h2>";
$columns_result = $conn->query("SHOW COLUMNS FROM course_reviews");
if ($columns_result) {
echo "<table><tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
$existing_columns = [];
while ($row = $columns_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>";
// Track existing columns
$existing_columns[$row['Field']] = true;
}
echo "</table>";
// Check for missing columns
echo "<h2>Column Check</h2>";
$required_columns = [
'id' => 'INT(11) NOT NULL AUTO_INCREMENT',
'course_id' => 'INT(11) NOT NULL',
'user_id' => 'INT(11) NOT NULL',
'rating' => 'INT(1) NOT NULL',
'comment' => 'TEXT',
'created_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP'
];
$missing_columns = [];
foreach ($required_columns as $column => $definition) {
if (!isset($existing_columns[$column])) {
$missing_columns[$column] = $definition;
}
}
if (empty($missing_columns)) {
echo "<div class='success'>All required columns exist.</div>";
} else {
echo "<div class='error'>Missing columns found: " . implode(', ', array_keys($missing_columns)) . "</div>";
// Offer to add missing columns
echo "<div class='actions'>";
echo "<form method='post' action=''>";
echo "<input type='hidden' name='add_missing_columns' value='1'>";
echo "<button type='submit' class='btn btn-primary'>Add Missing Columns</button>";
echo "</form>";
echo "</div>";
// Process adding missing columns
if (isset($_POST['add_missing_columns'])) {
echo "<h3>Adding Missing Columns</h3>";
foreach ($missing_columns as $column => $definition) {
$alter_sql = "ALTER TABLE course_reviews ADD COLUMN $column $definition";
$result = $conn->query($alter_sql);
if ($result) {
echo "<div class='success'>Successfully added '$column' column.</div>";
} else {
echo "<div class='error'>Failed to add '$column' column: " . $conn->error . "</div>";
}
}
echo "<div class='info'>Refresh this page to see the updated table structure.</div>";
}
}
} else {
echo "<div class='error'>Error fetching columns: " . $conn->error . "</div>";
}
// Offer to recreate the table
echo "<h2>Table Management</h2>";
echo "<div class='info'>If you're having persistent issues with the table structure, you can recreate the table. <strong>Warning: This will delete all existing reviews!</strong></div>";
echo "<div class='actions'>";
echo "<form method='post' action='' onsubmit=\"return confirm('Are you sure you want to drop and recreate the table? This will delete all existing reviews!')\">";
echo "<input type='hidden' name='recreate_table' value='1'>";
echo "<button type='submit' class='btn btn-danger'>Drop and Recreate Table</button>";
echo "</form>";
echo "</div>";
// Process recreating the table
if (isset($_POST['recreate_table'])) {
// Drop table
$drop_result = $conn->query("DROP TABLE course_reviews");
if ($drop_result) {
echo "<div class='success'>Successfully dropped the course_reviews table.</div>";
// Create table
$create_sql = "CREATE TABLE course_reviews (
id INT(11) NOT NULL AUTO_INCREMENT,
course_id INT(11) NOT NULL,
user_id INT(11) NOT NULL,
rating INT(1) NOT NULL,
comment TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_course_id (course_id),
KEY idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
$create_result = $conn->query($create_sql);
if ($create_result) {
echo "<div class='success'>Successfully recreated the course_reviews table with the correct structure.</div>";
echo "<div class='info'>Refresh this page to see the new table structure.</div>";
} else {
echo "<div class='error'>Failed to create table: " . $conn->error . "</div>";
}
} else {
echo "<div class='error'>Failed to drop table: " . $conn->error . "</div>";
}
}
} else {
echo "<div class='error'>The course_reviews table does not exist.</div>";
// Offer to create the table
echo "<div class='actions'>";
echo "<form method='post' action=''>";
echo "<input type='hidden' name='create_table' value='1'>";
echo "<button type='submit' class='btn btn-primary'>Create Table</button>";
echo "</form>";
echo "</div>";
// Process creating the table
if (isset($_POST['create_table'])) {
$create_sql = "CREATE TABLE course_reviews (
id INT(11) NOT NULL AUTO_INCREMENT,
course_id INT(11) NOT NULL,
user_id INT(11) NOT NULL,
rating INT(1) NOT NULL,
comment TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_course_id (course_id),
KEY idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
$create_result = $conn->query($create_sql);
if ($create_result) {
echo "<div class='success'>Successfully created the course_reviews table.</div>";
echo "<div class='info'>Refresh this page to see the table structure.</div>";
} else {
echo "<div class='error'>Failed to create table: " . $conn->error . "</div>";
}
}
}
echo "<div class='actions'>";
echo "<a href='course_reviews.php' class='btn btn-secondary'>Go to Course Reviews Page</a>";
echo "</div>";
echo "</body></html>";
?>