users.php12.1 KB
<?php
require 'config.php';
$page_title = "用户管理";
// 2. 处理表单提交(新增 / 编辑)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$role = $_POST['role'];
$id = $_POST['id'] ?? null; // 如果有ID,说明是编辑操作
try {
if (!empty($id)) {
// 编辑用户逻辑
if (!empty($_POST['password'])) {
// 如果填写了密码,则更新密码(必须加密存储)
$hashed_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$sql = "UPDATE users SET username = :username, email = :email, role = :role, password = :password WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':username' => $username, ':email' => $email, ':role' => $role,
':password' => $hashed_password, ':id' => $id
]);
} else {
// 未填写密码,保持原密码不变
$sql = "UPDATE users SET username = :username, email = :email, role = :role WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([':username' => $username, ':email' => $email, ':role' => $role, ':id' => $id]);
}
} else {
// 新增用户逻辑
if (empty($_POST['password'])) {
die('新增用户必须设置密码!');
}
$hashed_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$sql = "INSERT INTO users (username, email, password, role) VALUES (:username, :email, :password, :role)";
$stmt = $pdo->prepare($sql);
$stmt->execute([':username' => $username, ':email' => $email, ':password' => $hashed_password, ':role' => $role]);
}
header('Location: users.php'); exit;
} catch (PDOException $e) {
// 捕获唯一索引冲突等异常
die('操作失败:' . $e->getMessage());
}
}
// 3. 处理删除请求
if (isset($_GET['delete'])) {
$delete_id = $_GET['delete'];
// 防止误删自己
if ($delete_id == $_SESSION['user_id']) {
die('不能删除当前正在登录的账号!');
}
$stmt = $pdo->prepare("DELETE FROM users WHERE id = :id");
$stmt->execute([':id' => $delete_id]);
header('Location: users.php'); exit;
}
// 4. 获取所有用户数据
$stmt = $pdo->query("SELECT id, username, email, role FROM users ORDER BY id ASC");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 5. 处理编辑时的数据回显
$edit_user = null;
if (isset($_GET['edit'])) {
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute([':id' => $_GET['edit']]);
$edit_user = $stmt->fetch(PDO::FETCH_ASSOC);
}
// 6. 页面 HTML 输出部分
ob_start();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>评论管理</title>
<style>
</style>
</head>
<body>
<div class="overlay" id="overlay"></div>
<!-- 侧边栏 -->
<?php require_once __DIR__ . '/views/sidebar.php'; ?>
<!-- 主内容区 -->
<div class="main-wrapper">
<?php require_once __DIR__ . '/views/header.php'; ?>
<main class="content-area">
<div class="page-header">
<h1 class="page-title">数据概览</h1>
<button class="btn-primary"><a href="post_edit.php">+ 撰写新文章</a></button>
</div>
<!-- 数据表格 -->
<style>
/* ========== 表单卡片容器 ========== */
.user-form-card {
background: #ffffff;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
padding: 30px;
margin-bottom: 30px;
border: 1px solid #eef2f6;
}
.user-form-card h3 {
margin: 0 0 25px 0;
font-size: 20px;
color: #2c3e50;
padding-bottom: 15px;
border-bottom: 2px solid #f0f4f8;
}
/* ========== Grid 双列布局 ========== */
.user-form-grid {
display: grid;
grid-template-columns: 1fr 1fr; /* 默认两列平分 */
gap: 20px; /* 列与列、行与行之间的间距 */
}
/* 让密码和角色横跨两列 */
.user-form-grid .full-width {
grid-column: span 2;
}
/* 手机端自动变单列 */
@media (max-width: 600px) {
.user-form-grid {
grid-template-columns: 1fr;
}
.user-form-grid .full-width {
grid-column: span 1;
}
}
/* ========== 表单元素通用样式 ========== */
.user-form-card .form-group { margin-bottom: 0; } /* Grid 已处理间距,取消默认的 margin-bottom */
.user-form-card .form-group label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #4a5568;
font-size: 14px;
}
.user-form-card .form-control {
width: 100%;
padding: 12px 15px;
border: 1px solid #d1d9e6;
border-radius: 8px;
font-size: 15px;
color: #333;
background-color: #fafbfc;
transition: all 0.3s ease;
box-sizing: border-box;
}
.user-form-card .form-control:focus {
outline: none;
border-color: #4a90e2;
background-color: #fff;
box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.1);
}
/* ========== 按钮样式 ========== */
.form-actions {
margin-top: 25px;
display: flex;
align-items: center;
gap: 12px;
}
.btn {
padding: 12px 24px;
border: none;
border-radius: 8px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none;
display: inline-block;
text-align: center;
}
.btn-primary {
background: linear-gradient(135deg, #4a90e2, #357abd);
color: #fff;
}
.btn-primary:hover { background: linear-gradient(135deg, #357abd, #2a629e); transform: translateY(-1px); }
.btn-secondary {
background: #f0f4f8;
color: #5a6b7d;
border: 1px solid #d1d9e6;
}
.btn-secondary:hover { background: #e4e9f0; }
/* ========== 提示信息 ========== */
.alert-success {
background-color: #e6f9ee;
color: #1e7e34;
border: 1px solid #b2e6c4;
padding: 12px 18px;
border-radius: 8px;
margin-bottom: 20px;
font-weight: 600;
display: flex;
align-items: center;
gap: 8px;
}
.alert-success::before { content: "✅"; }
</style>
<!-- 提示信息 -->
<?php if (!empty($_GET['msg'])): ?>
<div class="alert-success">操作成功!</div>
<?php endif; ?>
<!-- ================= 1. 新增/编辑用户表单 ================= -->
<div class="user-form-card">
<h3>
<?php echo $edit_user ? '✏️ 编辑用户' : '➕ 添加新用户'; ?>
</h3>
<form method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($edit_user['id'] ?? ''); ?>">
<!-- 引入 Grid 容器 -->
<div class="user-form-grid">
<div class="form-group">
<label for="username">用户名 <span style="color:red;">*</span></label>
<input type="text" id="username" name="username" class="form-control" placeholder="请输入用户名" required
value="<?php echo htmlspecialchars($edit_user['username'] ?? ''); ?>">
</div>
<div class="form-group">
<label for="email">邮箱</label>
<input type="email" id="email" name="email" class="form-control" placeholder="请输入邮箱"
value="<?php echo htmlspecialchars($edit_user['email'] ?? ''); ?>">
</div>
<!-- 跨两列显示 -->
<div class="form-group full-width">
<label for="password">密码</label>
<input type="password" id="password" name="password" class="form-control"
placeholder="<?php echo $edit_user ? '留空则不修改密码' : '请输入密码 *'; ?>">
</div>
<!-- 跨两列显示 -->
<div class="form-group full-width">
<label for="role">角色</label>
<select id="role" name="role" class="form-control">
<option value="user" <?php echo ($edit_user['role'] ?? '') === 'user' ? 'selected' : ''; ?>>普通用户 (User)</option>
<option value="editor" <?php echo ($edit_user['role'] ?? '') === 'editor' ? 'selected' : ''; ?>>编辑 (Editor)</option>
<option value="admin" <?php echo ($edit_user['role'] ?? '') === 'admin' ? 'selected' : ''; ?>>管理员 (Admin)</option>
</select>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">
<?php echo $edit_user ? '💾 保存修改' : '➕ 添加新用户'; ?>
</button>
<?php if ($edit_user): ?>
<a href="users.php" class="btn btn-secondary">取消编辑</a>
<?php endif; ?>
</div>
</form>
</div>
<!-- ================= 2. 用户列表表格 ================= -->
<div class="card" style="padding: 0; overflow: hidden;">
<div class="table-wrapper">
<table>
<thead>
<tr>
<th style="width: 40px;"><input type="checkbox" class="table-checkbox"></th>
<th style="width: 60px;">ID</th>
<th>用户名</th>
<th>邮箱</th>
<th>角色</th>
<th style="width: 120px;">操作</th>
</tr>
</thead>
<tbody>
<?php if (!empty($users)): ?>
<?php foreach ($users as $user): ?>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td><?php echo $user['id']; ?></td>
<td>
<div class="article-title"><?php echo htmlspecialchars($user['username']); ?></div>
</td>
<td>
<div class="article-cat"><?php echo htmlspecialchars($user['email']); ?></div>
</td>
<td>
<!-- 角色标签:使用新的 status-badge 样式 -->
<?php
$role_map = [
'admin' => ['管理员', 'status-pub'],
'editor' => ['编辑', 'status-draft'],
'user' => ['普通用户', 'status-rej']
];
$role_st = $role_map[$user['role']] ?? [$user['role'], 'status-draft'];
echo "<span class='status-badge {$role_st[1]}'>{$role_st[0]}</span>";
?>
</td>
<td>
<!-- 操作按钮区域 -->
<a href="?edit=<?php echo $user['id']; ?>" class="action-btn" title="编辑">✏️</a>
<a href="?delete=<?php echo $user['id']; ?>" class="action-btn" title="删除" onclick="return confirm('确定要删除该用户吗?')">🗑️</a>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="6" style="text-align:center; color:#999; padding:30px 0;">暂无用户数据。</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</main>
</div>
<?php require_once __DIR__ . '/views/footer.php'; ?>
</body>
</html>