PHP - Guestbook Class

This class can be used to setup a simple guestbook. The class allows you to add comments, get comments and delete comments. This example uses a database to store the comments.
  1. <?php
  2.  
  3.     class GuestBook {
  4.         private $_pdo = null;
  5.  
  6.         public function __construct($host, $dbname, $username, $password){
  7.             try{
  8.                 $this->_pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
  9.             }catch(PDOException $e){
  10.                 throw new PDOException($e->getMessage(), $e->getCode(), $e);
  11.             }
  12.         }
  13.  
  14.         public function addComment($author, $subject, $comment){
  15.             if ($this->_pdo){
  16.                 $stm = $this->_pdo->prepare("INSERT INTO guest_book (author,subject,comment,posted_date) VALUES (:author,:subject,:comment,NOW())");
  17.                 $stm->bindParam(':author', $author);
  18.                 $stm->bindParam(':subject', $subject);
  19.                 $stm->bindParam(':comment', $comment);
  20.  
  21.                 if ($stm->execute()){
  22.                     return true;
  23.                 }
  24.             }
  25.             return false;
  26.         }
  27.  
  28.         public function getComments(){
  29.             if ($this->_pdo){
  30.                 $stm = $this->_pdo->prepare("SELECT * FROM guest_book");
  31.  
  32.                 if ($stm->execute()){
  33.                     return $stm->fetchAll(PDO::FETCH_OBJ);
  34.                 }
  35.             }
  36.             return false;
  37.         }
  38.  
  39.         public function getCommentById($comment_id){
  40.             if ($this->_pdo){
  41.                 $stm = $this->_pdo->prepare("SELECT * FROM guest_book WHERE comment_id=$comment_id");
  42.  
  43.                 if ($stm->execute()){
  44.                     return $stm->fetch(PDO::FETCH_OBJ);
  45.                 }
  46.             }
  47.             return false;
  48.         }
  49.  
  50.         public function deleteCommentById($comment_id){
  51.             if ($this->_pdo){
  52.                 $stm = $this->_pdo->prepare("DELETE FROM guest_book WHERE comment_id=$comment_id");
  53.  
  54.                 if ($stm->execute()){
  55.                     return true;
  56.                 }
  57.             }
  58.             return false;
  59.         }
  60.     }
  61.  
  62.     // Example usage
  63.  
  64.     try{
  65.         $book = new GuestBook('localhost', 'database', 'username', 'password');
  66.  
  67.         // Add a new comment
  68.         $book->addComment('John Smith', 'Hello', 'This is a comment');
  69.  
  70.         // Get all comments
  71.         $comments = $book->getComments();
  72.  
  73.         // Get comment by comment id
  74.         $comment = $book->getCommentById(1);
  75.  
  76.         // Delete comment by comment id
  77.         $book->deleteCommentById(2);
  78.  
  79.     }catch(PDOException $e){
  80.         // Print error message
  81.         echo $e->getMessage();
  82.     }
  83. ?>
  84.  
  85. //guest_book.sql
  86.  
  87. DROP TABLE IF EXISTS guest_book;
  88. CREATE TABLE guest_book (
  89.   comment_id int(11) NOT NULL AUTO_INCREMENT,
  90.   author varchar(80) DEFAULT NULL,
  91.   subject varchar(50) DEFAULT NULL,
  92.   comment text NOT NULL,
  93.   posted_date datetime NOT NULL,
  94.   PRIMARY KEY (comment_id)
  95. )

1 comment:

Unknown said...


Packers and Movers Bangalore @ http://www.buy5th.in/movers-and-packers-bangalore.html
Packers and Movers Hyderabad @ http://www.buy5th.in/movers-and-packers-hyderabad.html
Packers and Movers Delhi @ http://www.buy5th.in/movers-and-packers-delhi.html
Packers and Movers Pune @ http://www.buy5th.in/movers-and-packers-pune.html
Packers and Movers Ghaziabad @ http://www.buy5th.in/movers-and-packers-ghaziabad.html

Post a Comment