36952-vm/edit_user.php
Flatlogic Bot 76d7d99142 PMS 1
2025-12-15 01:31:18 +00:00

79 lines
2.6 KiB
PHP

<?php
require_once 'session.php';
check_admin();
require_once 'db/config.php';
$id = $_GET['id'] ?? null;
if (!$id) {
header("Location: users.php");
exit;
}
$db = db();
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
$user = $stmt->fetch();
if (!$user) {
header("Location: users.php?error=User not found");
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$role = $_POST['role'];
$password = $_POST['password'];
if ($username && $role) {
try {
if ($password) {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare("UPDATE users SET username = ?, password = ?, role = ? WHERE id = ?");
$stmt->execute([$username, $hashed_password, $role, $id]);
} else {
$stmt = $db->prepare("UPDATE users SET username = ?, role = ? WHERE id = ?");
$stmt->execute([$username, $role, $id]);
}
header("Location: users.php?success=User updated");
exit;
} catch (PDOException $e) {
$error = "Error: " . $e->getMessage();
}
} else {
$error = "Please fill all required fields.";
}
}
include 'templates/header.php';
?>
<div class="container mt-4">
<h2>Edit User</h2>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form action="edit_user.php?id=<?php echo $id; ?>" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required>
</div>
<div class="form-group">
<label for="password">Password (leave blank to keep current password)</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<div class="form-group">
<label for="role">Role</label>
<select class="form-control" id="role" name="role">
<option value="user" <?php echo ($user['role'] == 'user') ? 'selected' : ''; ?>>User</option>
<option value="admin" <?php echo ($user['role'] == 'admin') ? 'selected' : ''; ?>>Admin</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Update User</button>
<a href="users.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
<?php include 'templates/footer.php'; ?>