<?php
/**
* Database Table Update Script - Users Table
*
* This script adds missing columns to the users table that are required for
* proper functioning of the website.
*
* Important: This script should be run after installation or when upgrading
* from an older version of the system.
*
* Required columns added by this script:
* - profile_image: For storing user profile image paths
* - designation: For storing professional title or role
* - expertise: For storing areas of expertise (especially for instructors)
* - bio: For storing user biographical information
* - social_links: For storing social media links
*
* @version 1.0
* @date <?php echo date('Y-m-d'); ?>
*/
// Include database configuration
require_once '../../admin/database/db_config.php';
// Display header
echo "<!DOCTYPE html>
<html>
<head>
<title>Database Maintenance - Update Users 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; }
</style>
</head>
<body>
<div class='container'>
<h1>Update Users Table Structure</h1>
<p>This script adds missing columns to the users table that are necessary for the system to function properly.</p>";
try {
// Check if the users table exists
$table_exists = $conn->query("SHOW TABLES LIKE 'users'");
if ($table_exists->num_rows == 0) {
throw new Exception("Users table does not exist! Please run the installation script first.");
}
echo "<h2>Checking Required Columns</h2>";
echo "<table>
<tr>
<th>Column Name</th>
<th>Status</th>
<th>Description</th>
</tr>";
// Add profile_image column if it doesn't exist
$column_exists = $conn->query("SHOW COLUMNS FROM users LIKE 'profile_image'");
if ($column_exists->num_rows == 0) {
if ($conn->query("ALTER TABLE users ADD COLUMN profile_image VARCHAR(255) DEFAULT NULL")) {
echo "<tr>
<td>profile_image</td>
<td><span class='success'>Added Successfully</span></td>
<td>Stores path to user profile images</td>
</tr>";
} else {
echo "<tr>
<td>profile_image</td>
<td><span class='error'>Failed to Add</span></td>
<td>Error: " . $conn->error . "</td>
</tr>";
}
} else {
echo "<tr>
<td>profile_image</td>
<td><span class='info'>Already Exists</span></td>
<td>No action needed</td>
</tr>";
}
// Add designation column if it doesn't exist
$column_exists = $conn->query("SHOW COLUMNS FROM users LIKE 'designation'");
if ($column_exists->num_rows == 0) {
if ($conn->query("ALTER TABLE users ADD COLUMN designation VARCHAR(255) DEFAULT NULL")) {
echo "<tr>
<td>designation</td>
<td><span class='success'>Added Successfully</span></td>
<td>Stores professional title or role</td>
</tr>";
} else {
echo "<tr>
<td>designation</td>
<td><span class='error'>Failed to Add</span></td>
<td>Error: " . $conn->error . "</td>
</tr>";
}
} else {
echo "<tr>
<td>designation</td>
<td><span class='info'>Already Exists</span></td>
<td>No action needed</td>
</tr>";
}
// Add expertise column if it doesn't exist
$column_exists = $conn->query("SHOW COLUMNS FROM users LIKE 'expertise'");
if ($column_exists->num_rows == 0) {
if ($conn->query("ALTER TABLE users ADD COLUMN expertise TEXT DEFAULT NULL")) {
echo "<tr>
<td>expertise</td>
<td><span class='success'>Added Successfully</span></td>
<td>Stores areas of expertise (especially for instructors)</td>
</tr>";
} else {
echo "<tr>
<td>expertise</td>
<td><span class='error'>Failed to Add</span></td>
<td>Error: " . $conn->error . "</td>
</tr>";
}
} else {
echo "<tr>
<td>expertise</td>
<td><span class='info'>Already Exists</span></td>
<td>No action needed</td>
</tr>";
}
// Add bio column if it doesn't exist
$column_exists = $conn->query("SHOW COLUMNS FROM users LIKE 'bio'");
if ($column_exists->num_rows == 0) {
if ($conn->query("ALTER TABLE users ADD COLUMN bio TEXT DEFAULT NULL")) {
echo "<tr>
<td>bio</td>
<td><span class='success'>Added Successfully</span></td>
<td>Stores user biographical information</td>
</tr>";
} else {
echo "<tr>
<td>bio</td>
<td><span class='error'>Failed to Add</span></td>
<td>Error: " . $conn->error . "</td>
</tr>";
}
} else {
echo "<tr>
<td>bio</td>
<td><span class='info'>Already Exists</span></td>
<td>No action needed</td>
</tr>";
}
// Add social_links column if it doesn't exist
$column_exists = $conn->query("SHOW COLUMNS FROM users LIKE 'social_links'");
if ($column_exists->num_rows == 0) {
if ($conn->query("ALTER TABLE users ADD COLUMN social_links TEXT DEFAULT NULL")) {
echo "<tr>
<td>social_links</td>
<td><span class='success'>Added Successfully</span></td>
<td>Stores social media links</td>
</tr>";
} else {
echo "<tr>
<td>social_links</td>
<td><span class='error'>Failed to Add</span></td>
<td>Error: " . $conn->error . "</td>
</tr>";
}
} else {
echo "<tr>
<td>social_links</td>
<td><span class='info'>Already Exists</span></td>
<td>No action needed</td>
</tr>";
}
echo "</table>";
echo "<div class='success'>Users table structure has been checked and updated successfully.</div>";
echo "<p>This script is safe to run multiple times. It will only add columns that don't already exist.</p>";
echo "<div class='links'>
<a href='../../admin/index.php' class='btn'>Return to Admin Dashboard</a>
<a href='../../index.php' class='btn'>Go to Homepage</a>
</div>";
} catch (Exception $e) {
echo "<div class='error'>Error: " . $e->getMessage() . "</div>";
echo "<div class='links'>
<a href='../../admin/index.php' class='btn'>Return to Admin Dashboard</a>
<a href='../../index.php' class='btn'>Go to Homepage</a>
</div>";
}
echo "</div></body></html>";
?>