Sindbad~EG File Manager
<?php
/**
* Fix member_accounts table - Remove UNIQUE constraint on member_id if it exists
* and clean up any problematic records
*/
require_once 'config/config.php';
// Check if user is logged in and is a superuser
if (!isset($_SESSION['user_id']) || !isset($_SESSION['is_superuser']) || $_SESSION['is_superuser'] != 1) {
die("Access denied. Only superusers can run this script.");
}
$db = Database::getInstance()->getConnection();
$messages = [];
$errors = [];
echo "<!DOCTYPE html>
<html>
<head>
<title>Fix Member Accounts Table</title>
<style>
body { font-family: Arial, sans-serif; max-width: 1200px; margin: 20px auto; padding: 20px; }
.success { background: #d4edda; border: 1px solid #c3e6cb; color: #155724; padding: 10px; margin: 10px 0; border-radius: 5px; }
.error { background: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; padding: 10px; margin: 10px 0; border-radius: 5px; }
.info { background: #d1ecf1; border: 1px solid #bee5eb; color: #0c5460; padding: 10px; margin: 10px 0; border-radius: 5px; }
table { border-collapse: collapse; width: 100%; margin: 10px 0; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
.btn { display: inline-block; padding: 10px 20px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; margin: 10px 0; }
.btn:hover { background: #0056b3; }
</style>
</head>
<body>
<h1>Fix Member Accounts Table</h1>
<p>This script will check and fix the member_accounts table structure and data.</p>
";
try {
// Step 1: Check current table structure
echo "<h2>Step 1: Checking Table Structure</h2>";
$stmt = $db->query("SHOW CREATE TABLE member_accounts");
$result = $stmt->fetch();
$createTableSQL = $result['Create Table'];
echo "<div class='info'><strong>Current Table Structure:</strong><br><pre>" . htmlspecialchars($createTableSQL) . "</pre></div>";
// Check if there's a UNIQUE constraint on member_id
$hasUniqueConstraint = false;
if (preg_match('/UNIQUE.*member_id|member_id.*UNIQUE/i', $createTableSQL)) {
$hasUniqueConstraint = true;
echo "<div class='error'>⚠️ Found UNIQUE constraint on member_id column!</div>";
} else {
echo "<div class='success'>✓ No UNIQUE constraint found on member_id column.</div>";
}
// Step 2: Check for problematic data
echo "<h2>Step 2: Checking for Problematic Data</h2>";
$stmt = $db->query("SELECT COUNT(*) as count FROM member_accounts WHERE member_id = '' OR member_id IS NULL OR member_id = 0");
$emptyCount = $stmt->fetch();
if ($emptyCount['count'] > 0) {
echo "<div class='error'>Found {$emptyCount['count']} records with empty/NULL/0 member_id values</div>";
// Show these records
$stmt = $db->query("SELECT id, member_id, username, email, created_at FROM member_accounts WHERE member_id = '' OR member_id IS NULL OR member_id = 0 LIMIT 20");
$records = $stmt->fetchAll();
echo "<table>";
echo "<tr><th>ID</th><th>member_id</th><th>Username</th><th>Email</th><th>Created At</th></tr>";
foreach ($records as $record) {
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . htmlspecialchars($record['member_id'] ?? 'NULL') . "</td>";
echo "<td>" . htmlspecialchars($record['username']) . "</td>";
echo "<td>" . htmlspecialchars($record['email']) . "</td>";
echo "<td>" . $record['created_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<div class='success'>✓ No problematic records found.</div>";
}
// Step 3: Check for duplicate member_id values
echo "<h2>Step 3: Checking for Duplicate member_id Values</h2>";
$stmt = $db->query("
SELECT member_id, COUNT(*) as count
FROM member_accounts
WHERE member_id IS NOT NULL AND member_id != '' AND member_id != 0
GROUP BY member_id
HAVING count > 1
");
$duplicates = $stmt->fetchAll();
if (count($duplicates) > 0) {
echo "<div class='error'>Found " . count($duplicates) . " duplicate member_id values:</div>";
echo "<table>";
echo "<tr><th>member_id</th><th>Count</th></tr>";
foreach ($duplicates as $dup) {
echo "<tr><td>" . $dup['member_id'] . "</td><td>" . $dup['count'] . "</td></tr>";
}
echo "</table>";
} else {
echo "<div class='success'>✓ No duplicate member_id values found.</div>";
}
// Step 4: Get all indexes
echo "<h2>Step 4: Current Indexes</h2>";
$stmt = $db->query("SHOW INDEX FROM member_accounts");
$indexes = $stmt->fetchAll();
echo "<table>";
echo "<tr><th>Key Name</th><th>Column</th><th>Unique</th><th>Index Type</th></tr>";
foreach ($indexes as $index) {
$isUnique = $index['Non_unique'] == 0 ? 'YES' : 'NO';
$highlight = ($index['Column_name'] == 'member_id' && $isUnique == 'YES') ? ' style="background-color: #f8d7da;"' : '';
echo "<tr{$highlight}>";
echo "<td>" . htmlspecialchars($index['Key_name']) . "</td>";
echo "<td>" . htmlspecialchars($index['Column_name']) . "</td>";
echo "<td>" . $isUnique . "</td>";
echo "<td>" . htmlspecialchars($index['Index_type']) . "</td>";
echo "</tr>";
}
echo "</table>";
// Step 5: Offer to fix the issue
echo "<h2>Step 5: Fix Actions</h2>";
if (isset($_GET['action']) && $_GET['action'] == 'fix') {
echo "<div class='info'>Starting fix process...</div>";
// Find the unique key name on member_id
$uniqueKeyName = null;
foreach ($indexes as $index) {
if ($index['Column_name'] == 'member_id' && $index['Non_unique'] == 0) {
$uniqueKeyName = $index['Key_name'];
break;
}
}
if ($uniqueKeyName) {
try {
// Drop the UNIQUE constraint
$db->exec("ALTER TABLE member_accounts DROP INDEX `{$uniqueKeyName}`");
echo "<div class='success'>✓ Successfully removed UNIQUE constraint '{$uniqueKeyName}' on member_id column</div>";
// Re-add as regular index if it was named 'member_id'
if ($uniqueKeyName == 'member_id') {
// Check if idx_member_id already exists
$hasRegularIndex = false;
foreach ($indexes as $index) {
if ($index['Key_name'] == 'idx_member_id') {
$hasRegularIndex = true;
break;
}
}
if (!$hasRegularIndex) {
$db->exec("ALTER TABLE member_accounts ADD INDEX idx_member_id (member_id)");
echo "<div class='success'>✓ Added regular index 'idx_member_id' on member_id column</div>";
}
}
echo "<div class='success'><strong>✓ Fix Complete!</strong> You can now add members without the duplicate entry error.</div>";
echo "<a href='modules/membership/add.php' class='btn'>Go to Add Member</a>";
} catch (PDOException $e) {
echo "<div class='error'>Error during fix: " . $e->getMessage() . "</div>";
}
} else {
echo "<div class='info'>No UNIQUE constraint found to remove.</div>";
}
// Delete problematic records if requested
if (isset($_GET['delete_empty']) && $_GET['delete_empty'] == 'yes') {
try {
$stmt = $db->prepare("DELETE FROM member_accounts WHERE member_id = '' OR member_id IS NULL OR member_id = 0");
$stmt->execute();
$deletedCount = $stmt->rowCount();
echo "<div class='success'>✓ Deleted {$deletedCount} records with empty/NULL/0 member_id values</div>";
} catch (PDOException $e) {
echo "<div class='error'>Error deleting records: " . $e->getMessage() . "</div>";
}
}
} else {
// Show fix options
if ($hasUniqueConstraint || $emptyCount['count'] > 0 || count($duplicates) > 0) {
echo "<div class='info'>";
echo "<h3>Recommended Actions:</h3>";
echo "<ul>";
if ($hasUniqueConstraint) {
echo "<li>Remove UNIQUE constraint on member_id column (this is the main issue)</li>";
}
if ($emptyCount['count'] > 0) {
echo "<li>Delete {$emptyCount['count']} records with empty/NULL/0 member_id values</li>";
}
if (count($duplicates) > 0) {
echo "<li>Resolve " . count($duplicates) . " duplicate member_id values (manual intervention required)</li>";
}
echo "</ul>";
echo "<p><strong>Click the button below to automatically fix the issues:</strong></p>";
$deleteParam = ($emptyCount['count'] > 0) ? '&delete_empty=yes' : '';
echo "<a href='?action=fix{$deleteParam}' class='btn' onclick=\"return confirm('This will modify the database structure. Continue?');\">Run Auto-Fix</a>";
echo "</div>";
} else {
echo "<div class='success'><strong>✓ No issues found!</strong> The table structure looks good.</div>";
}
}
} catch (PDOException $e) {
echo "<div class='error'>Database Error: " . $e->getMessage() . "</div>";
}
echo "
<br><br>
<a href='dashboard.php' class='btn'>Back to Dashboard</a>
</body>
</html>";
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists