36 lines
923 B
PHP
36 lines
923 B
PHP
<?php
|
|
session_start();
|
|
require_once 'auth_check.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$user_id = $_GET['id'] ?? null;
|
|
if (!$user_id) {
|
|
header('Location: manage_users.php');
|
|
exit;
|
|
}
|
|
|
|
// Prevent admin from deleting their own account
|
|
if ($user_id == $_SESSION['user_id']) {
|
|
$_SESSION['error_message'] = 'You cannot delete your own account.';
|
|
header('Location: manage_users.php');
|
|
exit;
|
|
}
|
|
|
|
$pdoconfig = db();
|
|
$pdo = new PDO($pdoconfig['dsn'], $pdoconfig['user'], $pdoconfig['pass'], $pdoconfig['options']);
|
|
|
|
$stmt = $pdo->prepare('DELETE FROM users WHERE id = :id');
|
|
if ($stmt->execute(['id' => $user_id])) {
|
|
$_SESSION['success_message'] = 'User deleted successfully.';
|
|
} else {
|
|
$_SESSION['error_message'] = 'Failed to delete user.';
|
|
}
|
|
|
|
header('Location: manage_users.php');
|
|
exit;
|
|
?>
|