Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. if ($_SERVER["REQUEST_METHOD"] === "POST")
  2. {
  3. $title = $_POST["title"];
  4. $body = $_POST["body"];
  5. $slug = slugify($title);
  6.  
  7. if (empty(trim($title))) {
  8. $errors[] = "No title. Please enter a title.";
  9. } elseif (strlen($title) > MAX_LENGTH_TITLE) {
  10. $errors[] = "Title too long. Please enter a title less than or equal to " . MAX_LENGTH_TITLE . " characters.";
  11. }
  12. if (strlen($body) > MAX_LENGTH_BODY) {
  13. $errors[] = "Body too long. Please enter a body less than or equal to " . MAX_LENGTH_BODY . " characters.";
  14. }
  15.  
  16. if (!empty($_POST["edit-article"]))
  17. {
  18. if (slugify($title) !== $article["slug"]) {
  19. $errors[] = "Title may only change in capitalization or by having additional symbols added.";
  20. }
  21. if (empty($errors)) {
  22. $stmt = $pdo->prepare("UPDATE articles SET title = ?, body = ? WHERE id = ?");
  23. $stmt->execute([$title, $body, $article["id"]]);
  24. $_SESSION["message"] = "Article successfully updated.";
  25. header("Location: /wiki.php?title=" . $article["slug"]);
  26. exit();
  27. }
  28. } elseif (!empty($_POST["create-article"])) {
  29.  
  30. $stmt = $pdo->prepare("SELECT title, slug FROM articles WHERE title = ? OR slug = ?");
  31. $stmt->execute([$title, $slug]);
  32. $article_exists = $stmt->fetch();
  33.  
  34. if ($article_exists) {
  35. $errors[] = "An article by that title already exists. Please choose a different title.";
  36. }
  37. if (empty($errors)) {
  38. $stmt = $pdo->prepare("INSERT INTO articles (title, slug, body) VALUES (?, ?, ?)");
  39. $stmt->execute([$title, $slug, $body]);
  40. $_SESSION["message"] = "Article successfully created.";
  41. header("Location: /wiki.php?title=" . $slug);
  42. exit();
  43. }
  44. }
  45. }
  46. $title = $article["title"] ?? $title;
  47. $template = "edit.php";
  48. require_once "templates/layout.php";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement