Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/check_constraints.php

<?php
/**
 * Check Database Constraints
 * Verify membership_cards table structure and constraints
 */

require_once 'config/config.php';

try {
    $db = Database::getInstance()->getConnection();
    
    echo "<h2>Database Constraints Check</h2>";
    
    // Check if membership_cards table exists
    $stmt = $db->query("SHOW TABLES LIKE 'membership_cards'");
    if (!$stmt->fetch()) {
        echo "<p style='color: red;'>❌ membership_cards table does not exist!</p>";
        echo "<p>You need to run the database migration first.</p>";
        echo "<p><a href='database/run_migration.php'>Run Migration</a></p>";
        exit;
    }
    
    echo "<p style='color: green;'>✅ membership_cards table exists</p>";
    
    // Get table structure
    echo "<h3>membership_cards Table Structure:</h3>";
    $stmt = $db->query("DESCRIBE membership_cards");
    $columns = $stmt->fetchAll();
    
    echo "<table border='1' cellpadding='5' cellspacing='0'>";
    echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
    
    $memberIdColumn = null;
    foreach ($columns as $column) {
        echo "<tr>";
        echo "<td>" . htmlspecialchars($column['Field']) . "</td>";
        echo "<td>" . htmlspecialchars($column['Type']) . "</td>";
        echo "<td>" . htmlspecialchars($column['Null']) . "</td>";
        echo "<td>" . htmlspecialchars($column['Key']) . "</td>";
        echo "<td>" . htmlspecialchars($column['Default']) . "</td>";
        echo "<td>" . htmlspecialchars($column['Extra']) . "</td>";
        echo "</tr>";
        
        if ($column['Field'] === 'member_id') {
            $memberIdColumn = $column;
        }
    }
    echo "</table>";
    
    if ($memberIdColumn) {
        echo "<h3>member_id Column Analysis:</h3>";
        echo "<ul>";
        echo "<li><strong>Allows NULL:</strong> " . ($memberIdColumn['Null'] === 'YES' ? 'YES ✅' : 'NO ❌') . "</li>";
        echo "<li><strong>Default Value:</strong> " . ($memberIdColumn['Default'] ?: 'None') . "</li>";
        echo "</ul>";
        
        if ($memberIdColumn['Null'] === 'NO') {
            echo "<p style='color: red;'>❌ The member_id column does NOT allow NULL values!</p>";
            echo "<p>This is causing the constraint violation. We need to fix the table structure.</p>";
            
            // Offer to fix it
            if (isset($_GET['fix']) && $_GET['fix'] === 'true') {
                echo "<h3>Fixing member_id Column...</h3>";
                try {
                    $db->exec("ALTER TABLE membership_cards MODIFY COLUMN member_id INT NULL");
                    echo "<p style='color: green;'>✅ Fixed! member_id column now allows NULL values.</p>";
                } catch (PDOException $e) {
                    echo "<p style='color: red;'>❌ Failed to fix: " . htmlspecialchars($e->getMessage()) . "</p>";
                }
            } else {
                echo "<p><a href='?fix=true' style='background: #f56565; color: white; padding: 10px; text-decoration: none; border-radius: 5px;'>Fix member_id Column</a></p>";
            }
        } else {
            echo "<p style='color: green;'>✅ member_id column correctly allows NULL values.</p>";
        }
    } else {
        echo "<p style='color: red;'>❌ member_id column not found!</p>";
    }
    
    // Test creating a card with NULL member_id
    echo "<h3>Test Card Creation:</h3>";
    if (isset($_GET['test']) && $_GET['test'] === 'true') {
        try {
            $testCardNumber = 'TEST' . time();
            $stmt = $db->prepare("
                INSERT INTO membership_cards (member_id, card_number, issue_date, expiry_date, is_active)
                VALUES (NULL, :card_number, CURDATE(), DATE_ADD(CURDATE(), INTERVAL 5 YEAR), 1)
            ");
            $stmt->execute(['card_number' => $testCardNumber]);
            
            echo "<p style='color: green;'>✅ Successfully created test card with NULL member_id!</p>";
            
            // Clean up test card
            $db->prepare("DELETE FROM membership_cards WHERE card_number = :card_number")->execute(['card_number' => $testCardNumber]);
            echo "<p>Test card cleaned up.</p>";
            
        } catch (PDOException $e) {
            echo "<p style='color: red;'>❌ Failed to create test card: " . htmlspecialchars($e->getMessage()) . "</p>";
        }
    } else {
        echo "<p><a href='?test=true'>Test Card Creation with NULL member_id</a></p>";
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error: " . htmlspecialchars($e->getMessage()) . "</p>";
}

echo "<hr>";
echo "<p><a href='dashboard.php'>← Back to Dashboard</a></p>";
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists