Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="pl">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Blog</title>
- <style>
- .post {
- margin-bottom: 50px;
- }
- .post-content {
- margin-top: 30px;
- }
- </style>
- </head>
- <body>
- <h2>Najnowsze wpisy</h2>
- <?php
- $host = 'localhost';
- $dbName = 'blog';
- $user = 'root';
- $password = '';
- $connection = new mysqli($host, $user, $password, $dbName);
- //print_r($connection);
- $postList = $connection->query("SELECT post_title, post_date, post_content FROM post ORDER BY post_date DESC");
- while ( $post = mysqli_fetch_array($postList) ) {
- //print_r($post);
- echo '<article class="post">';
- echo '<h3>' . $post['post_title'] . '</h3>';
- echo '<span>' . $post['post_date'] . '</span>';
- echo '<div class="post-content">' . $post['post_content'] . '</div>';
- echo '</article>';
- }
- //dodawanie wpisów
- if ( isset($_POST['add-post']) ) {
- $title = $_POST['title'];
- $content = $_POST['content'];
- $categoryId = $_POST['category_id'];
- //print_r($title);
- $stm = $connection->prepare("INSERT INTO post (post_title, post_content, post_date, category_id) VALUES (?, ?, NOW(), ?)");
- $stm->bind_param("ssi", $title, $content, $categoryId);
- $stm->execute();
- $stm->close();
- header("Location: index.php");
- }
- ?>
- <h2>Dodaj nowy wpis</h2>
- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
- <label for="title">Tytuł</label><br>
- <input type="text" id="title" name="title"><br>
- <label for="content">Treść wpisu</label><br>
- <textarea name="content" id="content" cols="30" rows="8"></textarea><br>
- <label for="category_id">ID kategorii</label><br>
- <input type="number" id="category_id" name="category_id"><br>
- <input type="submit" name="add-post" value="Dodaj artykuł">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement