prepare("SELECT role FROM users WHERE id = ?"); $stmt->execute([$id]); $user = $stmt->fetch(); if ($user && $user['role'] === 'admin') { $stmt = $db->query("SELECT COUNT(*) FROM users WHERE role = 'admin'"); $admin_count = $stmt->fetchColumn(); if ($admin_count <= 1) { header("Location: users.php?error=Cannot delete the last admin"); exit; } } try { $stmt = $db->prepare("DELETE FROM users WHERE id = ?"); $stmt->execute([$id]); header("Location: users.php?success=User deleted"); exit; } catch (PDOException $e) { header("Location: users.php?error=Error deleting user"); exit; } } } else { header("Location: users.php"); exit; } ?>