81 lines
2.6 KiB
PHP
81 lines
2.6 KiB
PHP
<?php
|
|
require_once __DIR__.'/../includes/auth.php';
|
|
requireLogin();
|
|
|
|
// 处理表单提交
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
require_once __DIR__.'/../includes/db.php';
|
|
|
|
$action = $_POST['action'] ?? '';
|
|
$name = trim($_POST['name'] ?? '');
|
|
|
|
try {
|
|
if ($action === 'add') {
|
|
$stmt = $pdo->prepare("INSERT INTO categories (name) VALUES (?)");
|
|
$stmt->execute([$name]);
|
|
$msg = "分类添加成功";
|
|
} elseif ($action === 'delete') {
|
|
$stmt = $pdo->prepare("DELETE FROM categories WHERE id = ?");
|
|
$stmt->execute([$_POST['id']]);
|
|
$msg = "分类删除成功";
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = "操作失败:".$e->getMessage();
|
|
}
|
|
}
|
|
|
|
// 获取现有分类
|
|
$categories = $pdo->query("SELECT * FROM categories ORDER BY name")->fetchAll();
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>分类管理</title>
|
|
<link rel="stylesheet" href="../assets/css/admin.css">
|
|
</head>
|
|
<body>
|
|
<?php include '_sidebar.php'; ?>
|
|
|
|
<main class="content">
|
|
<h2>分类管理</h2>
|
|
|
|
<!-- 新增分类表单 -->
|
|
<div class="card">
|
|
<form method="POST">
|
|
<input type="text" name="name" placeholder="新分类名称" required>
|
|
<input type="hidden" name="action" value="add">
|
|
<button type="submit">添加分类</button>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- 分类列表 -->
|
|
<div class="card">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>分类名称</th>
|
|
<th>操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($categories as $cat): ?>
|
|
<tr>
|
|
<td><?= $cat['id'] ?></td>
|
|
<td><?= htmlspecialchars($cat['name']) ?></td>
|
|
<td>
|
|
<form method="POST"
|
|
onsubmit="return confirm('确认删除该分类?')">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="id" value="<?= $cat['id'] ?>">
|
|
<button type="submit" class="btn-danger">删除</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</main>
|
|
</body>
|
|
</html>
|