SHARE
TWEET

Untitled

a guest Oct 10th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. class Guestbook {
  5.     private $pdo;
  6.  
  7.     function __construct()
  8.     {
  9.         $this->pdo = new PDO('mysql:host=localhost;dbname=guestbook_test', 'root', '');
  10.     }
  11.  
  12.     public function saveGuestbookEntry($author, $email, $message)
  13.     {
  14.         if ($this->validateGuestbookEntry($author, $email, $message) === true) {
  15.             $cursor = $this->pdo->prepare('INSERT INTO guestbook_entries (author, email, message, create_date) VALUES (?, ?, ?, NOW())');
  16.             $cursor->execute(array($author, $email, $message));
  17.             $cursor->closeCursor();
  18.         } else {
  19.             $this->showError('Your input looks incorrect, please check it');
  20.         }
  21.     }
  22.  
  23.     private function validateGuestbookEntry($author, $email, $message)
  24.     {
  25.         if (
  26.             strlen($author) >= 2 &&
  27.             filter_var($email, FILTER_VALIDATE_EMAIL) &&
  28.             strlen($message) >= 5
  29.         ) {
  30.             return true;
  31.         } else {
  32.             return false;
  33.         }
  34.     }
  35.  
  36.     private function showError($message)
  37.     {
  38.         echo '<div class="error">' . $message . '</div>';
  39.     }
  40.  
  41.     public function getGuestbookEntries()
  42.     {
  43.         $cursor = $this->pdo->prepare('SELECT * FROM guestbook_entries ORDER BY create_date DESC');
  44.         $cursor->execute();
  45.         $result = $cursor->fetchAll(PDO::FETCH_ASSOC);
  46.         $cursor->closeCursor();
  47.  
  48.         return $result;
  49.     }
  50. }
  51.  
  52. $guestbookInstance = new Guestbook();
  53.  
  54. if (isset($_POST['submit'])) {
  55.     $guestbookInstance->saveGuestbookEntry($_POST['author'], $_POST['email'], $_POST['message']);
  56. }
  57.  
  58. ?>
  59. <!doctype html>
  60. <html>
  61.     <head>
  62.         <meta charset="UTF-8">
  63.     </head>
  64.     <body>
  65.         <section>
  66.             <form action="#" method="post">
  67.                 <input type="text" name="author" placeholder="Author"><br>
  68.                 <input type="text" name="email" placeholder="Email"><br>
  69.                 <textarea name="message" placeholder="Message"></textarea><br>
  70.                 <input type="submit" value="Send" name="submit">
  71.             </form>
  72.         </section>
  73.         <section>
  74.             <?php
  75.                 $entries =  $guestbookInstance->getGuestbookEntries();
  76.                 if (count($entries) > 0) {
  77.                     foreach ($entries as $entry) {
  78.                         echo "<div>";
  79.                         echo "<h3>{$entry['message']}</h3>";
  80.                         echo "<p>{$entry['author']}</p>";
  81.                         echo "</div>";
  82.                     }
  83.                 } else {
  84.                     echo "<p>No entries found.</p>";
  85.                 }
  86.             ?>
  87.         </section>
  88.     </body>
  89. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top