'danger', 'message' => 'Invalid input. Please check all fields.']; } else { // Prevent admin from changing their own role if they are the last admin if ($userId == $_SESSION['user_id'] && $role !== 'admin') { $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE role = 'admin'"); $stmt->execute(); $adminCount = $stmt->fetchColumn(); if ($adminCount <= 1) { $feedback = ['type' => 'danger', 'message' => 'You cannot change your role as you are the only admin.']; } } if (empty($feedback)) { $sql = "UPDATE users SET email = ?, role = ?"; $params = [$email, $role]; if (!empty($password)) { $sql .= ", password = ?"; $params[] = password_hash($password, PASSWORD_DEFAULT); } $sql .= " WHERE id = ?"; $params[] = $userId; $stmt = $pdo->prepare($sql); if ($stmt->execute($params)) { header('Location: users.php?update=success'); exit; } else { $feedback = ['type' => 'danger', 'message' => 'Failed to update user.']; } } } } // Fetch user data $stmt = $pdo->prepare("SELECT id, email, role FROM users WHERE id = ?"); $stmt->execute([$userId]); $user = $stmt->fetch(); if (!$user) { header('Location: users.php'); exit; } include 'header.php'; ?>

Edit User

Back to User List
Editing User: