categories.php9.9 KB
<?php
require 'config.php';
$msg = ''; // 用于显示操作成功/失败的提示
// 1. 处理新增和编辑表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$slug = trim($_POST['slug'] ?? '');
$id = $_POST['id'] ?? null;
if (!empty($name)) {
try {
if ($id) {
// 更新现有分类
$stmt = $pdo->prepare("UPDATE categories SET name = :name, slug = :slug WHERE id = :id");
$stmt->execute([':name' => $name, ':slug' => $slug, ':id' => $id]);
$msg = '分类修改成功!';
} else {
// 新增分类
$stmt = $pdo->prepare("INSERT INTO categories (name, slug) VALUES (:name, :slug)");
$stmt->execute([':name' => $name, ':slug' => $slug]);
$msg = '分类添加成功!';
}
header('Location: categories.php?msg=' . urlencode($msg)); exit;
} catch (PDOException $e) {
$msg = '操作失败:' . ($e->getCode() == 23000 ? '该分类别名(slug)已存在!' : $e->getMessage());
}
} else {
$msg = '分类名称不能为空!';
}
}
// 2. 处理删除请求
if (isset($_GET['delete_id'])) {
$id = $_GET['delete_id'];
try {
$stmt = $pdo->prepare("DELETE FROM categories WHERE id = :id");
$stmt->execute([':id' => $id]);
$msg = '分类删除成功!';
} catch (PDOException $e) {
// 如果外键约束报错(错误码 1451),说明该分类下还有文章
if ($e->getCode() == 23000) {
$msg = '删除失败:该分类下仍有文章,请先将文章移至其他分类或删除文章后再试!';
} else {
$msg = '删除失败:' . $e->getMessage();
}
}
header('Location: categories.php?msg=' . urlencode($msg)); exit;
}
// 3. 获取所有分类及其关联的文章数量(方便管理员了解情况)
$sql = "SELECT c.id, c.name, c.slug, COUNT(p.id) as post_count
FROM categories c
LEFT JOIN posts p ON c.id = p.category_id
GROUP BY c.id
ORDER BY c.id ASC";
$categories = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>
<!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>
<!-- 统计卡片 -->
<?php require_once __DIR__ . '/views/tj.php'; ?>
<!-- 数据表格 -->
<style>
/* ========== 分类表单卡片 ========== */
.category-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;
}
.category-form-card h3 {
margin: 0 0 25px 0;
font-size: 20px;
color: #2c3e50;
padding-bottom: 15px;
border-bottom: 2px solid #f0f4f8;
}
/* ========== 表单元素通用样式 ========== */
.category-form-card .form-group { margin-bottom: 20px; }
.category-form-card .form-group label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #4a5568;
font-size: 14px;
}
.category-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;
}
.category-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-hint {
font-size: 12px;
color: #8896a6;
margin-top: 6px;
}
/* ========== 按钮组 ========== */
.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;
}
</style>
<!-- 提示信息 -->
<?php if (!empty($_GET['msg'])): ?>
<div class="alert-success">✅ <?php echo htmlspecialchars($_GET['msg']); ?></div>
<?php endif; ?>
<!-- ================= 新增/编辑分类表单 ================= -->
<div class="category-form-card">
<h3 id="form-title">➕ 添加新分类</h3>
<form method="POST">
<input type="hidden" name="id" id="edit-id">
<div class="form-group">
<label for="edit-name">分类名称 <span style="color:red;">*</span></label>
<input type="text" name="name" id="edit-name" required class="form-control" placeholder="请输入分类名称">
</div>
<div class="form-group">
<label for="edit-slug">URL 别名 (Slug)</label>
<input type="text" name="slug" id="edit-slug" class="form-control" placeholder="如: tech-news">
<div class="form-hint">用于生成友好的 URL 路径,留空将自动生成。</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">💾 保存分类</button>
<button type="button" id="cancel-btn" onclick="resetForm()" class="btn btn-secondary" style="display:none;">取消编辑</button>
</div>
</form>
</div>
<!-- 分类列表 -->
<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>URL别名</th>
<th>文章数</th>
<th style="width: 120px;">操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($categories as $cat): ?>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td><?php echo $cat['id']; ?></td>
<td>
<div class="article-title"><?php echo htmlspecialchars($cat['name']); ?></div>
</td>
<td>
<div class="article-cat"><?php echo htmlspecialchars($cat['slug']); ?></div>
</td>
<td>
<span class="status-badge status-pub"><?php echo $cat['post_count']; ?> 篇</span>
</td>
<td>
<!-- 点击编辑时,通过 JS 将数据填入上方表单 -->
<a href="javascript:void(0);"
onclick="editCategory(<?php echo $cat['id']; ?>, '<?php echo addslashes(htmlspecialchars($cat['name'])); ?>', '<?php echo addslashes(htmlspecialchars($cat['slug'])); ?>')"
class="action-btn" title="编辑">✏️</a>
<a href="?delete_id=<?php echo $cat['id']; ?>"
class="action-btn" title="删除"
onclick="return confirm('确定要删除【<?php echo htmlspecialchars($cat['name']); ?>】吗?');">🗑️</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</main>
</div>
<?php require_once __DIR__ . '/views/footer.php'; ?>
<script>
// 简单的 JS 逻辑:实现无刷新编辑体验
function editCategory(id, name, slug) {
document.getElementById('edit-id').value = id;
document.getElementById('edit-name').value = name;
document.getElementById('edit-slug').value = slug;
document.getElementById('form-title').innerText = '编辑分类';
document.getElementById('cancel-btn').style.display = 'inline-block';
window.scrollTo(0, 0); // 滚动到顶部表单处
}
function resetForm() {
document.getElementById('edit-id').value = '';
document.getElementById('edit-name').value = '';
document.getElementById('edit-slug').value = '';
document.getElementById('form-title').innerText = '添加新分类';
document.getElementById('cancel-btn').style.display = 'none';
}
</script>
</body>
</html>