Files
myblog/admin/tag.php
T
2026-09-09 19:30:18 +08:00

55 lines
2.3 KiB
PHP

<?php
require_once __DIR__ . "/../includes/functions.php";
require_once __DIR__ . "/../includes/auth.php";
$adminPage = "tags";
$pageTitle = "标签管理";
require __DIR__ . "/_layout.php";
$msg = "";
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["name"])) {
$name = trim($_POST["name"]);
$slug = slugify($_POST["slug"] ?? "");
if ($name === "") { $msg = '<div class="msg">标签名不能为空</div>'; }
else {
$s = db()->prepare("INSERT INTO tags (name, slug) VALUES (?,?)");
$s->execute([$name, $slug]);
header("Location: tag.php?ok=1"); exit;
}
}
if (isset($_GET["del"])) {
$tid = (int)$_GET["del"];
db()->prepare("DELETE FROM article_tags WHERE tag_id=?")->execute([$tid]);
db()->prepare("DELETE FROM tags WHERE id=?")->execute([$tid]);
header("Location: tag.php?deleted=1"); exit;
}
$list = db()->query("SELECT t.*, (SELECT COUNT(*) FROM article_tags at WHERE at.tag_id=t.id) AS cnt FROM tags t ORDER BY t.id DESC")->fetchAll();
?>
<div class="admin-head"><h1>标签管理</h1></div>
<?php echo $msg; ?><?php if (isset($_GET["deleted"])) { ?><div class="msg ok">删除成功</div><?php } ?>
<div class="card glass form-card" style="margin-bottom:20px">
<h3>新增标签</h3>
<form method="post">
<div class="form-row">
<div class="field"><label>名称</label><input class="input" type="text" name="name" required></div>
<div class="field"><label>别名(slug,可选)</label><input class="input" type="text" name="slug" placeholder="english-slug"></div>
</div>
<button class="glass-btn primary" type="submit">添加</button>
</form>
</div>
<div class="card glass">
<table class="admin-table">
<thead><tr><th>ID</th><th>名称</th><th>别名</th><th>使用次数</th><th>操作</th></tr></thead>
<tbody>
<?php foreach($list as $t): ?>
<tr>
<td><?php echo $t["id"]; ?></td>
<td><?php echo e($t["name"]); ?></td>
<td class="text-muted"><?php echo e($t["slug"]); ?></td>
<td><?php echo $t["cnt"]; ?></td>
<td><div class="actions"><a href="?del=<?php echo $t["id"]; ?>" data-confirm="删除标签将解除所有文章的该标签关联,继续?">删除</a></div></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php require __DIR__ . "/_tail.php"; ?>