Path : /home/vishqocm/pcib.in/
File Upload :
Current File : //home/vishqocm/pcib.in/create_missing_tables.php

<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Include database configuration
require_once 'admin/database/db_config.php';

// Get error message if passed
$error_message = isset($_GET['error']) ? urldecode($_GET['error']) : '';

// Tables to create
$tables = [
    'enrollments' => "CREATE TABLE IF NOT EXISTS enrollments (
        id INT(11) NOT NULL AUTO_INCREMENT,
        student_id INT(11) NOT NULL,
        course_id INT(11) NOT NULL,
        enrollment_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        status ENUM('in_progress', 'completed', 'cancelled') NOT NULL DEFAULT 'in_progress',
        progress INT(3) NOT NULL DEFAULT 0,
        completion_date DATETIME NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        UNIQUE KEY (student_id, course_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
    
    'instructors' => "CREATE TABLE IF NOT EXISTS instructors (
        id INT(11) NOT NULL AUTO_INCREMENT,
        name VARCHAR(100) NOT NULL,
        email VARCHAR(100) NOT NULL,
        bio TEXT NULL,
        image VARCHAR(255) NULL,
        expertise VARCHAR(255) NULL,
        social_facebook VARCHAR(255) NULL,
        social_twitter VARCHAR(255) NULL,
        social_linkedin VARCHAR(255) NULL,
        status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        UNIQUE KEY (email)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
    
    'course_reviews' => "CREATE TABLE IF NOT EXISTS 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,
        review TEXT NULL,
        status ENUM('pending', 'approved', 'rejected') NOT NULL DEFAULT 'approved',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        UNIQUE KEY (course_id, user_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
];

// Default data
$default_data = [
    'instructors' => [
        "INSERT INTO instructors (name, email, bio, image, expertise, status) 
         VALUES 
         ('John Smith', '[email protected]', 'Experienced web developer with over 10 years of industry experience.', 'instructor-1.jpg', 'Web Development, JavaScript, React', 'active'),
         ('Sarah Johnson', '[email protected]', 'UI/UX designer with a passion for creating beautiful and functional interfaces.', 'instructor-2.jpg', 'UI/UX Design, Adobe XD, Figma', 'active'),
         ('Michael Brown', '[email protected]', 'Full-stack developer specializing in PHP and MySQL applications.', 'instructor-3.jpg', 'PHP, MySQL, Laravel', 'active')"
    ]
];

// Create tables
$created_tables = [];
$errors = [];

foreach ($tables as $table_name => $create_sql) {
    // Check if table exists
    $result = mysqli_query($conn, "SHOW TABLES LIKE '$table_name'");
    $table_exists = $result && mysqli_num_rows($result) > 0;
    
    if (!$table_exists) {
        // Create the table
        if (mysqli_query($conn, $create_sql)) {
            $created_tables[] = $table_name;
            
            // Insert default data if available
            if (isset($default_data[$table_name])) {
                foreach ($default_data[$table_name] as $insert_sql) {
                    if (!mysqli_query($conn, $insert_sql)) {
                        $errors[] = "Error inserting default data into $table_name: " . mysqli_error($conn);
                    }
                }
            }
        } else {
            $errors[] = "Error creating table $table_name: " . mysqli_error($conn);
        }
    }
}

// HTML output
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Missing Tables</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .header {
            background: linear-gradient(135deg, #4e73df 0%, #224abe 100%);
            color: white;
            padding: 20px 0;
            margin-bottom: 30px;
        }
        .card {
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            border: none;
            border-radius: 0.35rem;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="header text-center">
        <h1><i class="fas fa-database me-2"></i> Create Missing Tables</h1>
        <p>This utility creates necessary database tables for the LMS system</p>
    </div>
    
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-8 offset-md-2">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h4>Create Missing Tables</h4>
                    </div>
                    <div class="card-body">
                        <?php if (!empty($error_message)): ?>
                            <div class="alert alert-danger">
                                <h5>Error from previous operation:</h5>
                                <p><?php echo $error_message; ?></p>
                            </div>
                        <?php endif; ?>
                        
                        <?php if (!empty($created_tables)): ?>
                            <div class="alert alert-success">
                                <h5>Tables Created Successfully:</h5>
                                <ul>
                                    <?php foreach ($created_tables as $table): ?>
                                        <li><?php echo $table; ?></li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-info">
                                <h5>No tables needed to be created.</h5>
                            </div>
                        <?php endif; ?>
                        
                        <?php if (!empty($errors)): ?>
                            <div class="alert alert-danger">
                                <h5>Errors:</h5>
                                <ul>
                                    <?php foreach ($errors as $error): ?>
                                        <li><?php echo $error; ?></li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php endif; ?>
                        
                        <div class="text-center mt-3">
                            <a href="courses.php" class="btn btn-primary">Go to Courses</a>
                            <a href="index.php" class="btn btn-secondary">Go to Homepage</a>
                            <a href="admin/dashboard.php" class="btn btn-info">Go to Admin Dashboard</a>
                            <a href="admin/database/fix_courses_table.php" class="btn btn-warning">Fix Courses Table</a>
                            <a href="admin/database/fix_activities_table.php" class="btn btn-danger">Fix Activities Table</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Bootstrap JS Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>