Sindbad~EG File Manager

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

<?php
require_once 'config/config.php';

$db = Database::getInstance()->getConnection();

echo "<h2>Checking for 'member_id' Column Issue</h2>";
echo "<hr>";

// Check if member_id column exists in members table
echo "<h3>Step 1: Check if 'member_id' column exists in members table</h3>";
try {
    $stmt = $db->query("SHOW COLUMNS FROM members WHERE Field = 'member_id'");
    $column = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($column) {
        echo "<p style='color: red;'><strong>❌ FOUND 'member_id' column!</strong></p>";
        echo "<pre>";
        print_r($column);
        echo "</pre>";
    } else {
        echo "<p style='color: green;'><strong>✓ No 'member_id' column found (good)</strong></p>";
    }
} catch (PDOException $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}

// Check all indexes on members table
echo "<h3>Step 2: Check all indexes on members table</h3>";
try {
    $stmt = $db->query("SHOW INDEX FROM members");
    $indexes = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>Key Name</th><th>Column</th><th>Unique?</th><th>Type</th></tr>";
    
    $foundMemberId = false;
    foreach ($indexes as $index) {
        $isUnique = $index['Non_unique'] == 0 ? 'YES (UNIQUE)' : 'No';
        $color = $index['Non_unique'] == 0 ? 'background-color: #ffe6e6;' : '';
        
        if ($index['Column_name'] == 'member_id') {
            $foundMemberId = true;
            $color = 'background-color: #ffcccc; font-weight: bold;';
        }
        
        echo "<tr style='$color'>";
        echo "<td>{$index['Key_name']}</td>";
        echo "<td>{$index['Column_name']}</td>";
        echo "<td>$isUnique</td>";
        echo "<td>{$index['Index_type']}</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    if ($foundMemberId) {
        echo "<p style='color: red; font-size: 18px; font-weight: bold;'>⚠️ PROBLEM FOUND: There's an index on 'member_id' column!</p>";
    }
} catch (PDOException $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}

// Check for empty values in member_id if it exists
echo "<h3>Step 3: Check for empty/duplicate values</h3>";
try {
    // First check if column exists before querying
    $stmt = $db->query("SHOW COLUMNS FROM members WHERE Field = 'member_id'");
    if ($stmt->fetch()) {
        $stmt = $db->query("SELECT COUNT(*) as count FROM members WHERE member_id = '' OR member_id IS NULL");
        $result = $stmt->fetch();
        
        if ($result['count'] > 0) {
            echo "<p style='color: red;'>Found {$result['count']} members with empty member_id</p>";
        } else {
            echo "<p style='color: green;'>No empty member_id values</p>";
        }
        
        // Check for duplicates
        $stmt = $db->query("
            SELECT member_id, COUNT(*) as count 
            FROM members 
            WHERE member_id IS NOT NULL AND member_id != ''
            GROUP BY member_id 
            HAVING count > 1
        ");
        $duplicates = $stmt->fetchAll();
        
        if (count($duplicates) > 0) {
            echo "<p style='color: red;'>Found " . count($duplicates) . " duplicate member_id values</p>";
        } else {
            echo "<p style='color: green;'>No duplicate member_id values</p>";
        }
    } else {
        echo "<p style='color: green;'>No member_id column exists to check</p>";
    }
} catch (PDOException $e) {
    echo "<p style='color: orange;'>Note: " . $e->getMessage() . "</p>";
}

echo "<hr>";
echo "<h3>SOLUTION:</h3>";
echo "<p>If a 'member_id' column was found above, we need to DROP it because:</p>";
echo "<ul>";
echo "<li>It's NOT in the original schema (database/schema.sql)</li>";
echo "<li>The PRIMARY KEY is 'id' (auto-increment)</li>";
echo "<li>The membership card number is stored in 'membershipcard_id'</li>";
echo "</ul>";

echo "<form method='post' style='margin: 20px 0;'>";
echo "<button type='submit' name='fix_now' style='padding: 15px 30px; background: #dc2626; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; font-weight: bold;'>DROP member_id Column & Indexes</button>";
echo "</form>";

if (isset($_POST['fix_now'])) {
    echo "<h3>Applying Fix...</h3>";
    
    try {
        // First drop any indexes on member_id
        $stmt = $db->query("SHOW INDEX FROM members WHERE Column_name = 'member_id'");
        $indexes = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($indexes as $index) {
            $keyName = $index['Key_name'];
            if ($keyName != 'PRIMARY') {
                echo "<p>Dropping index: $keyName</p>";
                $db->exec("ALTER TABLE members DROP INDEX `$keyName`");
            }
        }
        
        // Then drop the column if it exists
        $stmt = $db->query("SHOW COLUMNS FROM members WHERE Field = 'member_id'");
        if ($stmt->fetch()) {
            echo "<p>Dropping member_id column...</p>";
            $db->exec("ALTER TABLE members DROP COLUMN member_id");
            echo "<p style='color: green; font-size: 18px; font-weight: bold;'>✓ Successfully removed member_id column!</p>";
        }
        
        echo "<p style='color: green;'><strong>✓ Fix complete! Please try adding a member again.</strong></p>";
        echo "<p><a href='modules/membership/add.php' style='padding: 10px 20px; background: #1E40AF; color: white; text-decoration: none; border-radius: 5px;'>Go to Add Member Page</a></p>";
        
    } catch (PDOException $e) {
        echo "<p style='color: red;'><strong>Error:</strong> " . $e->getMessage() . "</p>";
    }
}
?>

<style>
    body {
        font-family: Arial, sans-serif;
        max-width: 1000px;
        margin: 20px auto;
        padding: 20px;
        background: #f5f5f5;
    }
    h2 { color: #1E40AF; }
    h3 { color: #333; margin-top: 20px; }
    table { background: white; width: 100%; border-collapse: collapse; }
    th { background: #1E40AF; color: white; padding: 10px; }
    td { padding: 8px; }
</style>

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