Path : /home/vishqocm/pcib.in/admin/database/
File Upload :
Current File : /home/vishqocm/pcib.in/admin/database/create_tables.php

<?php
// Include database configuration
require_once 'db_config.php';

// Function to create a table if it doesn't exist
function create_table_if_not_exists($conn, $table_name, $table_sql) {
    $check_table = mysqli_query($conn, "SHOW TABLES LIKE '$table_name'");
    if (mysqli_num_rows($check_table) == 0) {
        if (mysqli_query($conn, $table_sql)) {
            echo "Table '$table_name' created successfully.<br>";
        } else {
            echo "Error creating table '$table_name': " . mysqli_error($conn) . "<br>";
        }
    } else {
        echo "Table '$table_name' already exists.<br>";
    }
}

// Activities table
$activities_table = "CREATE TABLE activities (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    user_type VARCHAR(50),
    activity_type VARCHAR(50),
    activity_description TEXT,
    ip_address VARCHAR(50) DEFAULT NULL,
    course_title VARCHAR(255) DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

// Wishlist table
$wishlist_table = "CREATE TABLE wishlist (
    id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT NOT NULL,
    course_id INT NOT NULL,
    added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY student_course (student_id, course_id),
    FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE,
    FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE
)";

// Enrollment Applications table for the multi-step process
$enrollment_applications_table = "CREATE TABLE enrollment_applications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT NOT NULL,
    course_id INT NOT NULL,
    step INT NOT NULL DEFAULT 1,
    status VARCHAR(50) NOT NULL DEFAULT 'pending',
    personal_details JSON,
    document_uploads JSON,
    payment_details JSON,
    verification_token VARCHAR(64),
    is_verified TINYINT(1) DEFAULT 0,
    verification_date TIMESTAMP NULL,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE,
    FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE
)";

// Student Documents table
$student_documents_table = "CREATE TABLE student_documents (
    id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT NOT NULL,
    application_id INT NOT NULL,
    document_type VARCHAR(100) NOT NULL,
    file_name VARCHAR(255) NOT NULL,
    file_path VARCHAR(255) NOT NULL,
    uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    status VARCHAR(50) DEFAULT 'pending',
    FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE,
    FOREIGN KEY (application_id) REFERENCES enrollment_applications(id) ON DELETE CASCADE
)";

// Create the activities table if it doesn't exist
create_table_if_not_exists($conn, 'activities', $activities_table);

// Create the wishlist table if it doesn't exist
create_table_if_not_exists($conn, 'wishlist', $wishlist_table);

// Create the enrollment applications table if it doesn't exist
create_table_if_not_exists($conn, 'enrollment_applications', $enrollment_applications_table);

// Create the student documents table if it doesn't exist
create_table_if_not_exists($conn, 'student_documents', $student_documents_table);

// Create settings table if it doesn't exist
$sql_settings = "CREATE TABLE IF NOT EXISTS settings (
    id INT(11) NOT NULL AUTO_INCREMENT,
    setting_key VARCHAR(100) NOT NULL,
    setting_value TEXT,
    setting_description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY (setting_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

create_table_if_not_exists($conn, 'settings', $sql_settings);

// Insert default settings if they don't exist
$default_settings = [
    ['school_name', 'Popular Computer Institute', 'School Name'],
    ['school_address', 'Your Institute Address, City, State, PIN', 'School Address'],
    ['school_phone', '+91 9876 543 210', 'School Phone Number'],
    ['school_email', '[email protected]', 'School Email Address'],
    ['school_logo', 'assets/img/logo.png', 'School Logo Path'],
    ['school_website', 'https://www.popularcomputerinstitute.com', 'School Website URL']
];

foreach ($default_settings as $setting) {
    $check_query = "SELECT id FROM settings WHERE setting_key = ?";
    $stmt = $conn->prepare($check_query);
    $stmt->bind_param("s", $setting[0]);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows == 0) {
        $insert_query = "INSERT INTO settings (setting_key, setting_value, setting_description) VALUES (?, ?, ?)";
        $stmt = $conn->prepare($insert_query);
        $stmt->bind_param("sss", $setting[0], $setting[1], $setting[2]);
        $stmt->execute();
        echo "Added default setting: " . $setting[0] . "<br>";
    }
}

echo "<p>Database setup completed.</p>";
echo "<p><a href='../../index.php'>Return to homepage</a></p>";
?>