Advertisement
Guest User

Untitled

a guest
Oct 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pl">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Blog</title>
  8. <style>
  9. .post {
  10. margin-bottom: 50px;
  11. }
  12. .post-content {
  13. margin-top: 30px;
  14. }
  15. </style>
  16.  
  17. </head>
  18.  
  19. <body>
  20. <h2>Najnowsze wpisy</h2>
  21.  
  22. <?php
  23.  
  24. $host = 'localhost';
  25. $dbName = 'blog';
  26. $user = 'root';
  27. $password = '';
  28.  
  29. $connection = new mysqli($host, $user, $password, $dbName);
  30. //print_r($connection);
  31. $postList = $connection->query("SELECT post_title, post_date, post_content FROM post ORDER BY post_date DESC");
  32. while ( $post = mysqli_fetch_array($postList) ) {
  33. //print_r($post);
  34. echo '<article class="post">';
  35. echo '<h3>' . $post['post_title'] . '</h3>';
  36. echo '<span>' . $post['post_date'] . '</span>';
  37. echo '<div class="post-content">' . $post['post_content'] . '</div>';
  38. echo '</article>';
  39. }
  40. //dodawanie wpisów
  41. if ( isset($_POST['add-post']) ) {
  42. $title = $_POST['title'];
  43. $content = $_POST['content'];
  44. $categoryId = $_POST['category_id'];
  45. //print_r($title);
  46. $stm = $connection->prepare("INSERT INTO post (post_title, post_content, post_date, category_id) VALUES (?, ?, NOW(), ?)");
  47. $stm->bind_param("ssi", $title, $content, $categoryId);
  48. $stm->execute();
  49. $stm->close();
  50. header("Location: index.php");
  51.  
  52. }
  53.  
  54.  
  55. ?>
  56.  
  57. <h2>Dodaj nowy wpis</h2>
  58. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  59. <label for="title">Tytuł</label><br>
  60. <input type="text" id="title" name="title"><br>
  61. <label for="content">Treść wpisu</label><br>
  62. <textarea name="content" id="content" cols="30" rows="8"></textarea><br>
  63. <label for="category_id">ID kategorii</label><br>
  64. <input type="number" id="category_id" name="category_id"><br>
  65. <input type="submit" name="add-post" value="Dodaj artykuł">
  66. </form>
  67.  
  68. </body>
  69. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement