<?php
// Include the header
include 'includes/header.php';
// Check if the user is an admin
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
echo '<div class="alert alert-danger">Access denied. You must be an admin to access this page.</div>';
include 'includes/footer.php';
exit;
}
// Include database connection
require_once __DIR__ . '/../config.php';
// Function to execute SQL file
function execute_sql_file($file_path) {
global $conn;
// Read SQL file
$sql = file_get_contents($file_path);
// Split SQL into individual queries
$queries = array_filter(array_map('trim', explode(';', $sql)));
// Execute each query
foreach ($queries as $query) {
if (!empty($query)) {
try {
if (!$conn->query($query)) {
echo "<div class='alert alert-warning'>Warning: " . $conn->error . "</div>";
}
} catch (mysqli_sql_exception $e) {
// Skip duplicate column errors
if ($e->getCode() == 1060) {
echo "<div class='alert alert-info'>Info: Column already exists - skipping.</div>";
} else {
echo "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>";
}
}
}
}
}
// Start capturing output
ob_start();
?>
<div class="container mt-5">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4>Database Update</h4>
</div>
<div class="card-body">
<div class="alert alert-info">
<p>This page will update your database structure to include all the latest fields and tables for the Learning Management System.</p>
</div>
<div class="update-log">
<?php
// Update users table
echo "<h5>Updating users table structure...</h5>";
execute_sql_file(__DIR__ . '/database/update_users.sql');
echo "<hr>";
// Update courses table
echo "<h5>Updating courses table structure...</h5>";
include_once __DIR__ . '/database/alter_courses_table.php';
echo "<hr>";
// Get the output
$output = ob_get_clean();
// Display the output
echo '<div class="update-results">';
echo $output;
echo '</div>';
?>
</div>
<div class="text-center mt-4">
<a href="index.php" class="btn btn-primary">Return to Dashboard</a>
</div>
</div>
</div>
</div>
</div>
</div>
<style>
.update-results {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 4px;
padding: 15px;
max-height: 400px;
overflow-y: auto;
}
.update-results h5 {
color: #0d6efd;
margin-top: 20px;
}
.update-results hr {
margin: 15px 0;
}
.alert {
margin-bottom: 10px;
}
</style>
<?php
// Include the footer
include 'includes/footer.php';
?>