Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ($_SERVER["REQUEST_METHOD"] === "POST")
- {
- $title = $_POST["title"];
- $body = $_POST["body"];
- $slug = slugify($title);
- if (empty(trim($title))) {
- $errors[] = "No title. Please enter a title.";
- } elseif (strlen($title) > MAX_LENGTH_TITLE) {
- $errors[] = "Title too long. Please enter a title less than or equal to " . MAX_LENGTH_TITLE . " characters.";
- }
- if (strlen($body) > MAX_LENGTH_BODY) {
- $errors[] = "Body too long. Please enter a body less than or equal to " . MAX_LENGTH_BODY . " characters.";
- }
- if (!empty($_POST["edit-article"]))
- {
- if (slugify($title) !== $article["slug"]) {
- $errors[] = "Title may only change in capitalization or by having additional symbols added.";
- }
- if (empty($errors)) {
- $stmt = $pdo->prepare("UPDATE articles SET title = ?, body = ? WHERE id = ?");
- $stmt->execute([$title, $body, $article["id"]]);
- $_SESSION["message"] = "Article successfully updated.";
- header("Location: /wiki.php?title=" . $article["slug"]);
- exit();
- }
- } elseif (!empty($_POST["create-article"])) {
- $stmt = $pdo->prepare("SELECT title, slug FROM articles WHERE title = ? OR slug = ?");
- $stmt->execute([$title, $slug]);
- $article_exists = $stmt->fetch();
- if ($article_exists) {
- $errors[] = "An article by that title already exists. Please choose a different title.";
- }
- if (empty($errors)) {
- $stmt = $pdo->prepare("INSERT INTO articles (title, slug, body) VALUES (?, ?, ?)");
- $stmt->execute([$title, $slug, $body]);
- $_SESSION["message"] = "Article successfully created.";
- header("Location: /wiki.php?title=" . $slug);
- exit();
- }
- }
- }
- $title = $article["title"] ?? $title;
- $template = "edit.php";
- require_once "templates/layout.php";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement