37497-vm/edit_user.php
Flatlogic Bot 12b49853d7 2
2026-01-19 09:07:59 +00:00

108 lines
4.3 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'Admin') {
header('Location: user_management.php?error=unauthorized');
exit;
}
$user_id = $_GET['id'] ?? null;
if (!$user_id) {
header('Location: user_management.php');
exit;
}
$pdo = db();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$role_id = $_POST['role_id'] ?? '';
$division_id = $_POST['division_id'] ?? null;
$department_id = $_POST['department_id'] ?? null;
if (empty($role_id)) {
header('Location: edit_user.php?id=' . $user_id . '&error=missing_fields');
exit;
}
try {
$stmt = $pdo->prepare("UPDATE users SET role_id = ?, division_id = ?, department_id = ? WHERE id = ?");
$stmt->execute([$role_id, $division_id, $department_id, $user_id]);
header('Location: user_management.php?success=user_updated');
exit;
} catch (PDOException $e) {
header('Location: edit_user.php?id=' . $user_id . '&error=db_error');
exit;
}
}
try {
// Fetch user data along with their division, department, and role IDs
$stmt = $pdo->prepare("SELECT id, username, email, role_id, department_id, division_id FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
if (!$user) {
header('Location: user_management.php?error=user_not_found');
exit;
}
// Fetch all divisions
$divisions = $pdo->query("SELECT id, name FROM divisions ORDER BY name")->fetchAll();
} catch (PDOException $e) {
die('Database error: ' . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit User</title>
<style>
body { font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background-color: #F7F9FC; color: #333; margin: 0; padding: 20px; }
.container { max-width: 500px; margin: 0 auto; background-color: #FFFFFF; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 20px; }
h1 { color: #4A90E2; }
.form-label { display: block; margin-bottom: 5px; font-weight: bold; }
.form-control, .form-select { width: 100%; padding: 8px; margin-bottom: 10px; border-radius: 4px; border: 1px solid #ccc; }
.btn { padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; color: white; }
.btn-primary { background-color: #4A90E2; }
.btn-secondary { background-color: #6c757d; }
</style>
</head>
<body>
<div class="container mt-5">
<h1>Edit User: <?php echo htmlspecialchars($user['username']); ?></h1>
<?php if (isset($_GET['error'])): ?>
<div class="alert alert-danger">An error occurred. Please try again.</div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<label for="division_id" class="form-label">Division</label>
<select class="form-select" id="division_id" name="division_id" required>
<option value="">Select Division</option>
<?php foreach ($divisions as $division): ?>
<option value="<?php echo $division['id']; ?>" <?php echo ($division['id'] == $user['division_id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($division['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="department_id" class="form-label">Department</label>
<select class="form-select" id="department_id" name="department_id" required data-initial="<?php echo htmlspecialchars($user['department_id']); ?>">
<option value="">Select Department</option>
</select>
</div>
<div class="mb-3">
<label for="role_id" class="form-label">Role</label>
<select class="form-select" id="role_id" name="role_id" required data-initial="<?php echo htmlspecialchars($user['role_id']); ?>">
<option value="">Select Role</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Update User</button>
<a href="user_management.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
<script src="assets/js/edit_user.js?v=<?php echo time(); ?>"></script>
</body>
</html>