108 lines
5.1 KiB
PHP
108 lines
5.1 KiB
PHP
<?php
|
|
require_once 'header.php';
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in and is an admin
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
|
|
header("Location: profile.php");
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Handle user updates
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['update_role'])) {
|
|
$user_id = $_POST['user_id'];
|
|
$role = $_POST['role'];
|
|
$stmt = $pdo->prepare("UPDATE users SET role = ? WHERE id = ?");
|
|
$stmt->execute([$role, $user_id]);
|
|
} elseif (isset($_POST['update_balance'])) {
|
|
$user_id = $_POST['user_id'];
|
|
$balance = $_POST['balance'];
|
|
$stmt = $pdo->prepare("UPDATE users SET balance = ? WHERE id = ?");
|
|
$stmt->execute([$balance, $user_id]);
|
|
} elseif (isset($_POST['update_status'])) {
|
|
$user_id = $_POST['user_id'];
|
|
$status = $_POST['status'];
|
|
$stmt = $pdo->prepare("UPDATE users SET status = ? WHERE id = ?");
|
|
$stmt->execute([$status, $user_id]);
|
|
}
|
|
header("Location: admin_users.php");
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->query("SELECT * FROM users ORDER BY created_at DESC");
|
|
$users = $stmt->fetchAll();
|
|
?>
|
|
|
|
<header class="hero text-center">
|
|
<div class="container">
|
|
<h1 class="display-4">Manage Users</h1>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container my-5">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Username</th>
|
|
<th>Email</th>
|
|
<th>Role</th>
|
|
<th>Balance</th>
|
|
<th>Status</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($users as $user): ?>
|
|
<tr>
|
|
<td><?php echo $user['id']; ?></td>
|
|
<td><?php echo htmlspecialchars($user['username']); ?></td>
|
|
<td><?php echo htmlspecialchars($user['email']); ?></td>
|
|
<td>
|
|
<form action="admin_users.php" method="POST" class="d-inline">
|
|
<input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
|
|
<select name="role" class="form-select form-select-sm" onchange="this.form.submit()">
|
|
<option value="user" <?php echo $user['role'] === 'user' ? 'selected' : ''; ?>>User</option>
|
|
<option value="admin" <?php echo $user['role'] === 'admin' ? 'selected' : ''; ?>>Admin</option>
|
|
</select>
|
|
<input type="hidden" name="update_role" value="1">
|
|
</form>
|
|
</td>
|
|
<td>
|
|
<form action="admin_users.php" method="POST" class="d-inline">
|
|
<input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
|
|
<div class="input-group input-group-sm">
|
|
<input type="number" step="0.01" name="balance" class="form-control" value="<?php echo htmlspecialchars($user['balance']); ?>">
|
|
<button type="submit" name="update_balance" class="btn btn-outline-primary">Save</button>
|
|
</div>
|
|
</form>
|
|
</td>
|
|
<td>
|
|
<form action="admin_users.php" method="POST" class="d-inline">
|
|
<input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
|
|
<select name="status" class="form-select form-select-sm" onchange="this.form.submit()">
|
|
<option value="active" <?php echo $user['status'] === 'active' ? 'selected' : ''; ?>>Active</option>
|
|
<option value="banned" <?php echo $user['status'] === 'banned' ? 'selected' : ''; ?>>Banned</option>
|
|
</select>
|
|
<input type="hidden" name="update_status" value="1">
|
|
</form>
|
|
</td>
|
|
<td>
|
|
<a href="admin_user_edit.php?id=<?php echo $user['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php require_once 'footer.php'; ?>
|