93 lines
3.2 KiB
PHP
93 lines
3.2 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
require_once '../db/config.php';
|
|
|
|
$user_id = $_GET['id'] ?? null;
|
|
if (!$user_id) {
|
|
header('Location: users.php');
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Handle form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$role = $_POST['role'];
|
|
$password = $_POST['password'];
|
|
|
|
try {
|
|
if (!empty($password)) {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare('UPDATE users SET username = ?, role = ?, password = ? WHERE id = ?');
|
|
$stmt->execute([$username, $role, $hashed_password, $user_id]);
|
|
} else {
|
|
$stmt = $pdo->prepare('UPDATE users SET username = ?, role = ? WHERE id = ?');
|
|
$stmt->execute([$username, $role, $user_id]);
|
|
}
|
|
header('Location: users.php?updated=true');
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
$error_message = "Error updating user: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
// Fetch user data
|
|
try {
|
|
$stmt = $pdo->prepare('SELECT username, role FROM users WHERE id = ?');
|
|
$stmt->execute([$user_id]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if (!$user) {
|
|
header('Location: users.php');
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
die("Error fetching user data: " . $e->getMessage());
|
|
}
|
|
|
|
$page_title = "Edit User";
|
|
include 'partials/header.php';
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<h3 class="text-dark mb-4">Edit User</h3>
|
|
|
|
<?php if (!empty($error_message)): ?>
|
|
<div class="alert alert-danger"><?php echo $error_message; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card shadow">
|
|
<div class="card-header py-3">
|
|
<p class="text-primary m-0 fw-bold">Edit User Details</p>
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="edit_user.php?id=<?php echo $user_id; ?>" method="POST">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">New Password (leave blank to keep current password)</label>
|
|
<input type="password" class="form-control" id="password" name="password">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="role" class="form-label">Role</label>
|
|
<select class="form-select" id="role" name="role">
|
|
<option value="admin" <?php echo ($user['role'] === 'admin') ? 'selected' : ''; ?>>Admin</option>
|
|
<option value="editor" <?php echo ($user['role'] === 'editor') ? 'selected' : ''; ?>>Editor</option>
|
|
</select>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Save Changes</button>
|
|
<a href="users.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'partials/footer.php'; ?>
|