prepare("SELECT * FROM articles WHERE id=?");
$stmt->execute([$id]);
$article = $stmt->fetch();
if (!$article) { echo '
文章不存在或已删除
'; echo '返回列表
'; require __DIR__ . "/_tail.php"; exit; }
}
$cats = get_categories();
$allTags = get_all_tags();
$curTags = $article ? array_column(get_article_tags($article["id"]), "id") : [];
$msg = "";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$title = trim($_POST["title"] ?? "");
$slug = trim($_POST["slug"] ?? "");
$category_id = (int)($_POST["category_id"] ?? 0);
$excerpt = trim($_POST["excerpt"] ?? "");
$content = $_POST["content"] ?? "";
$status = ($_POST["status"] ?? "published") === "draft" ? "draft" : "published";
$tags = $_POST["tags"] ?? []; if (!is_array($tags)) { $tags = []; }
if ($title === "" || $content === "") {
$msg = '标题和内容不能为空
';
} else {
if ($slug === "") { $slug = slugify($title); }
if ($msg === "") {
$user_id = current_user()["id"];
if ($id) {
$s = db()->prepare("UPDATE articles SET title=?, slug=?, category_id=?, excerpt=?, content=?, status=? WHERE id=?");
$s->execute([$title, $slug, $category_id ?: null, $excerpt, $content, $status, $id]);
} else {
$s = db()->prepare("INSERT INTO articles (user_id, title, slug, category_id, excerpt, content, status, views) VALUES (?,?,?,?,?,?,?,0)");
$s->execute([$user_id, $title, $slug, $category_id ?: null, $excerpt, $content, $status]);
$id = (int)db()->lastInsertId();
}
db()->prepare("DELETE FROM article_tags WHERE article_id=?")->execute([$id]);
if ($tags) {
$si = db()->prepare("INSERT IGNORE INTO article_tags (article_id, tag_id) VALUES (?,?)");
foreach ($tags as $tid) { $si->execute([$id, (int)$tid]); }
}
header("Location: article_list.php?ok=1"); exit;
}
}
}
?>