Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Guest book</title>
  5.     </head>
  6.         <?php
  7.        
  8.             if(isset($_POST['author'])) {
  9.                 $author = htmlspecialchars($_POST['author']);
  10.                 $email = htmlspecialchars($_POST['email']);
  11.                 $message = htmlspecialchars($_POST['message']);
  12.                 if(isset($_POST['author']) && isset($_POST['email']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== false) {
  13.                     try {
  14.                         $conn = new PDO('mysql:host=localhost;dbname=guestbook', 'root', '1234');
  15.                         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.                         $insert = "INSERT INTO messages (author, email, message) VALUES ('$author', '$email', '$message')";
  17.                         $conn->exec($insert);
  18.                         $select = "SELECT * FROM messages ORDER BY id";
  19.                         $num = 1;
  20.                         foreach ($conn->query($select, PDO::FETCH_ASSOC)->fetchAll() as $row) {
  21.                             echo $num++ . '<br>';
  22.                             echo $row['author'] . '<br>';
  23.                             echo '<a href="mailto:' . $row['email'] . '">' . $row['email'] . '</a>' . '<br>';
  24.                             echo $row['message'] . '<br>';
  25.                             echo '<a href="index.php?act=del&id=' . $row['id'] . '">Delete Message</a>' . '<hr>';
  26.                             $id = $_GET['id'];
  27. var_dump($_GET);
  28.     var_dump($_POST);
  29.                             if($_GET['act'] == 'del' && is_numeric($_GET['id']) == true) {
  30.                                 $delete = "DELETE FROM messages WHERE id='$id'";
  31.                                 $conn->exec($delete);
  32.    
  33.                             }
  34.                             else {
  35.                                 echo 'Cannot delete ' . '<br>';
  36.                             }
  37.                         }
  38.                     }  
  39.                     catch(PDOException $e) {
  40.                         echo $e->getMessage();
  41.                     }
  42.                 }
  43.                 else {
  44.                     echo 'Error while adding your message';
  45.                 }
  46.             }
  47.         ?>
  48.         <form id="form" action="index.php" method="post">
  49.             <span>Author: <input type="text" name="author" required></span>
  50.             <span>Email: <input type="text" name="email" required></span>
  51.             <span>Message: <input type="text" name="message" required></span>  
  52.             <input type="submit">
  53.         </form>
  54.     </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement