How to make a PHP Guestbook?

Any problem with PHP can be disscused here
Post Reply
anish
Posts: 353
Joined: Fri Apr 27, 2007 12:34 pm
Contact:

How to make a PHP Guestbook?

Post by anish »

I will show you how to make a guestbook on your site. I will use a text file to store the data that the guests come with.

MySQL
I use a table, tbGuestBook, to store the data into.

Code: Select all

CREATE TABLE `tbGuestBook` (
`ID` INT AUTO_INCREMENT,
`Name` CHAR( 75 ) NOT NULL ,
`Topic` CHAR( 50 ) NOT NULL ,
UNIQUE (
`ID` 
)
);



display.php
First I make a basic html page where the user can write name, topic and message. You can see this site here

insert.php
When you submit the form in display.php, the data must be stored someplace. Here I will use a textfile to store the message and store the title and name in a database. If you are not familiar with textfiles or databases and php, you can read the tutorials in the php section concerning this.

Code: Select all

<?
$name    = $HTTP_POST_VARS['txtName'];
$title   = $HTTP_POST_VARS['txtTitle'];
$message = $HTTP_POST_VARS['txtaMessage'];

/*******************************************************************************
											puts name and title into the table
*******************************************************************************/
//include file for the wrapperclass
include "wrapperclass.php";

//make an object of the wrapperclass
$guest=new GuestInterface();

//make db connection
$guestpointer=$guest->connect();

//insert record
$guest->addToGuestBook($name, $title);

$rs = $guest->getUniqID($name, $title);

if ($row=mysql_fetch_assoc($rs))
{
  $filename = "guestfiles/guest".$row['ID'].".txt";
}

//disconnect to db
$guest->Disconnect($guestpointer);

/*******************************************************************************
											puts message into a file
*******************************************************************************/

//make file and insert content
$guest->makeFile($filename, $message);

//Redirct to the display php
header("Location: display.php");

?>

wrapperclass.php
In order to not get to confused, I have wrapped the methods for updating the database and textfile into a class. This class is used in the insert.php.

Code: Select all

<?php
		 
/****************************************************
      Wrapper class around mysql database. 
*****************************************************/
class GuestInterface
{

  /****************************************************
      Construct
  *****************************************************/
  function GuestInterface()
  {


  }

	/*****************************************************************************
      Methods for the database
  ******************************************************************************/

	
  /****************************************************
      Connects to database
  *****************************************************/
  function Connect()
  {
      $host = "localhost";
      $username = "usr";
      $password = "psw";
      $database = "mydb";
      $server = mysql_connect($host, $username, $password) or die(mysql_error());			
      $connection = mysql_select_db($database, $server);
	
      return $server;
  }
 
  /****************************************************
      Disconnect from database
  *****************************************************/
  function Disconnect($server)
  {
   	mysql_close($server);
  }


	
	/****************************************************
      Get info from all the users
  *****************************************************/
	function getGuestBook()
	{
	 		$rs = mysql_query("SELECT * FROM tutGuestBook ORDER BY ID DESC") or die(mysql_error());
		  return $rs;
	}
	
	/****************************************************
      Get a uniq id that we will use for making the file
  *****************************************************/
	function getUniqID($name, $title)
	{
	 		$rs = mysql_query("SELECT * FROM tutGuestBook WHERE Name = '$name' AND Topic = '$title' ORDER BY ID DESC") or die(mysql_error());
		  return $rs;
	}

  /****************************************************
      Add to guestbook
  *****************************************************/
  function addToGuestBook($name, $title)
  {
      $sql = mysql_query("INSERT INTO tutGuestBook VALUES ( '', '$name', '$title'  ) ")or die(mysql_error());	
  }


 	/*****************************************************************************
      Methods for making and deleting files
  ******************************************************************************/
  
	
	/****************************************************
      This method make a file, and takes the filename as an argument
  *****************************************************/
	function makeFile($filename, $content)
	{
	  $content = stripslashes($content);
	  $fp = fopen($filename,"w") or die ("Error opening file in write mode!");
	  fputs($fp,$content);
	  fclose($fp) or die ("Error closing file!");
	}
	

	/****************************************************
      This method display the data in a file
  *****************************************************/
	function displayFile($filename)
	{
    if (!file_exists($filename)) 
		{ 
    	echo "Couldn't find datafile, please contact administrator!"; 
    } 
    else 
		{ 
    	$newfile = fopen($filename,"r"); 
    	$content = fread($newfile, filesize($filename)); 
   	fclose($newfile); 
    } 


    $content = stripslashes($content); 
    $content = htmlentities($content); 
    $content = nl2br($content); 
    
    echo $content; 
  }
	
}
?>
listguests.php
I have made an own file that lists the content the guests have written. I include this in the display.php so the content get listed under the guestbook.

Code: Select all

<?
//include file for the wrapperclass
include "wrapperclass.php";

//make an object of the wrapperclass
$guest=new GuestInterface();

//make db connection
$guestpointer=$guest->connect();

$rs = $guest->getGuestBook();

while ($row=mysql_fetch_assoc($rs))
{
?>



<table summary="" border="0">
<tr class="head">
<td width="420"><? 	echo($row['Name']); ?></td>
</tr>
<tr class="main">
<td><? 	echo($row['Topic']); ?></td>
</tr>
<tr class="main">
<td>
<? 	
$guest->displayFile("guestfiles/guest".$row['ID'].".txt");
?>
</td>
</tr>

</table>  

<?
}

//disconnect to db
$guest->Disconnect($guestpointer);

?>
And now Upload to you website and your Guest book is ready to be used.


Post Reply