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

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

// Include database configuration
require_once __DIR__ . '/../../config.php';

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Function to check if column exists
function column_exists($conn, $table, $column) {
    $query = "SHOW COLUMNS FROM {$table} LIKE '{$column}'";
    $result = $conn->query($query);
    return $result && $result->num_rows > 0;
}

// Add certificate columns to enrollments table if they don't exist
$certificate_columns = [
    'certificate_number' => 'VARCHAR(50) NULL',
    'certificate_path' => 'VARCHAR(255) NULL',
    'certificate_issue_date' => 'DATETIME NULL',
    'verification_code' => 'VARCHAR(20) NULL',
    'certificate_data' => 'LONGTEXT NULL',
    'certificate_meta' => 'TEXT NULL'
];

echo "Checking and adding certificate columns to enrollments table...<br>";

$success = true;

foreach ($certificate_columns as $column => $definition) {
    if (!column_exists($conn, 'enrollments', $column)) {
        $alter_query = "ALTER TABLE enrollments ADD COLUMN {$column} {$definition}";
        
        if ($conn->query($alter_query)) {
            echo "Added column '{$column}' to enrollments table. ✓<br>";
        } else {
            echo "Error adding column '{$column}': " . $conn->error . " ✗<br>";
            $success = false;
        }
    } else {
        echo "Column '{$column}' already exists in enrollments table. ✓<br>";
    }
}

// Add index on certificate number for faster lookups
if (!$conn->query("SHOW INDEX FROM enrollments WHERE KEY_NAME = 'idx_certificate_number'")) {
    if ($conn->query("CREATE INDEX idx_certificate_number ON enrollments(certificate_number)")) {
        echo "Created index on certificate_number column. ✓<br>";
    } else {
        echo "Error creating index on certificate_number: " . $conn->error . " ✗<br>";
        $success = false;
    }
} else {
    echo "Index on certificate_number already exists. ✓<br>";
}

// Add index on verification code for faster verification lookups
if (!$conn->query("SHOW INDEX FROM enrollments WHERE KEY_NAME = 'idx_verification_code'")) {
    if ($conn->query("CREATE INDEX idx_verification_code ON enrollments(verification_code)")) {
        echo "Created index on verification_code column. ✓<br>";
    } else {
        echo "Error creating index on verification_code: " . $conn->error . " ✗<br>";
        $success = false;
    }
} else {
    echo "Index on verification_code already exists. ✓<br>";
}

if ($success) {
    echo "<br>Successfully updated enrollments table with certificate columns!<br>";
} else {
    echo "<br>Some errors occurred while updating the enrollments table.<br>";
}

// Close the connection
$conn->close();
?>