vinuthomas.com
Toggle Content .:: Home :: Community Forums :: Image Gallery :: Blogs :: My Account ::. Toggle Content

Menu[x]
 Home Community Members options Forums Tools
 My Tech Blog
 Tutorials

Ads[x]

News Feed[x]
Get our RSS/XML News Feed Here
News & Articles:
RSS Feed
Latest @ the Forums:
RSS Feed

Content >> PHP & MYSQL >> Coding a simple Guestbook using PHP and MySQL

Coding a simple Guestbook using PHP and MySQL

By Vinu Thomas

This is a short article which shows you how to make a simple guestbook by capturing the visitor's input and storing the data in a database using PHP. You'll need to know a bit of HTML, PHP and MySQL in order to do the task. For a guestbook to work, we'll need to create 3 pages.

  1. We'll need a form to prompt the visitor to leave a comment in your guestbook. Let's call this page comments.php .
  2. We'll need to create some PHP and MySQL code to capture the visitor's input and save it to the database. Let's call this page, store_comment.php.
  3. Finally, we need to show the comments the visitors have submitted as our guest book. This page will be called guestbook.php since this will be our main guestbook page.

Get the Visitor's Details and Comments ( comments.php)

Let's start off by making a sample form with the data to be captured using HTML. We'll begin by listing out the data we wish to capture. A general guestbook captures the following data :

  • Visitors Name
  • Email Id
  • Comment and
  • Country

Let's make the simple guestbook form in HTML to look like the following submission box:

Your Name
Your Email ID
Country   
Comments  

This form should submit the data to the second part of our code which will be saved in store_comment.php . So the action of the form should be targeted at save_db.php. The simplle HTML code for the form would look like this :

<form name="form1" method="post" action="store_comment.php">

  Your Name: <input name="name" type="text" size="15"> <br>
  Your Email ID: <input name="email" type="text" size="15"> <br>
  Country: <input name="country" type="text" size="15"> <br>
  Comments : <textarea name="comments" cols="15" rows="3"></textarea> <br>
  <input type="submit" name="Submit" value="Submit">

</form>

You can add additional styling to the form code to display the form according to your liking. If you want the code which I used to create the look of the form above, you can use the following code:

<form name="form1" method="post" action="store_comment.php">

<table width="40%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td width="22%">Your Name
</td>
<td width="78%">
<input name="name" type="text" size="15">
</td>
</tr>
<tr align="left" valign="top">
<td>Your Email ID</td>
<td><input name="email" type="text" size="15"></td>
</tr>
<tr align="left" valign="top">
<td>Country&nbsp;&nbsp;&nbsp;</td>
<td><input name="country" type="text" size="15"></td>
</tr>
<tr align="left" valign="top">
<td>Comments &nbsp;</td>
<td><textarea name="comments" cols="15" rows="3" ></textarea></td>
</tr>
<tr align="left" valign="top">
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Submit">
</div></td>
</tr>
</table>

</form>

You can save this HTML code as comments.php

The Database Table

Before we can capture and store this data into the database, we'll need to create a table to store the data (I'm assuming that you already have a database created and ready to use).

CREATE TABLE guestbook (
id int(11) NOT NULL auto_increment,
name text NOT NULL,
email varchar(80) NOT NULL default '',
v_comment varchar(255) default NULL,
country varchar(40) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;

You can create the table based on the above code either using mysqladmin utility or the PHPMyAdmin interface.

Storing the Visitor's Details & Comments ( store_comment.php )

Before we can write any information to the database, we'll need to connect to the database from PHP. This can be achieved using the mysql_connect() function. The syntax for this function is

mysql_connect(database_host, user_name, password)

Once we connect to the MySQL database, we'll need to select the database in which our tables are created. This is done using the mysql_select_db() functtion, and the syntax for this function is

mysql_select_db(database_name, database_connector);

Now let's get to coding the actual data insertion process.

<? // code for store_comment.php

// Database information required to connect to database
$host="localhost";
$name = "dbuser";
$pass = "dbpass";
$dbname = "yourdatabase";

// Connect to Database and select the database to use
$dbi = mysql_connect($host, $name,$pass) or
     die("I cannot connect to the database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);

// Get the values posted from the Form in comments.php
$name = $_POST["name"];
$email = $_POST["email"];
$country = $_POST["country"];
$comment = $_POST["comments"];

//Check if a name & comment have been entered

if ($name=="")
{
  die (" You haven't Entered Your Name. Go back and Enter your Name"); 
}

if ($comment=="")
{
  die (" You haven't Entered any comment. Go back and enter some comment to be stored");
}

// Insert the Details into the Database
$sql = "INSERT INTO guestbook (name,email,v_comment,country) VALUES ('$name','$email','$comment','$country')";
$result = mysql_query($sql,$dbi);
If ($result)
   {
      echo "<center> Your Details have been added to the database<br>";
      echo " <a href=\"guestbook.php\">Click here to go back to the guestbook</a></center>" ;
   } else
         echo " Your details were not added due to some database problem";

?>

This code can be saved as store_comment.php. Before you upload this file to your server, you should modify the values for $host, $name, $pass and $dbname to suit your web-host's database details.

Viewing the GuestBook Comments (guestbook.php)

Once you have made the entry form for guests to leave comments, you should make a guestbook page to retreive the contents of the guestbook from the database and show in a HTML page.

<? // Source for guestbook.php
// Database information required to connect to database
$host="localhost";
$name = "dbuser";
$pass = "dbpass";
$dbname = "yourdatabase";

// Connect to Database and select the database to use
$dbi = mysql_connect($host, $name,$pass) or
     die("I cannot connect to the database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);

// Display the top of the page for the guestbook
echo " <center><h1> A Simple Guestbook in PHP & MYSQL </h1></center><br>";
echo " <a href=\"comments.php\">Click here to Leave us a message</a><br><br>";
echo " <b>Comments from Visitors </b><hr>";

// Fetch and Display the Results from the Database
$result = mysql_query("select name, email, country, v_comment from guestbook ORDER BY id",$dbi);

while ($myrow = mysql_fetch_array($result))
{
   echo "<b>name:</b> $myrow[0]<br> <b>email: </b>$myrow[1]<br> <b>Country:</b> $myrow[2] <br><b>message:</b> $myrow[3] ";
}

echo("<hr> This guest book is coded in PHP and uses MySQL to store data ");

?>

Make the same changes to the variables $host, $name, $pass and $dbname, as you have done in store_comment.php, and upload the file. You can invoke your newly made guestbook by calling guestbook.php on your server.

Creative Commons License
This work is licensed under a Creative Commons License.

 



Site Search[x]

Login[x]
Welcome Guest

Nickname

At The Forum[x]
Last 10 Forum Messages

BSNL broadband flaky connection / cycling connect-disconnect
Last post by raviagrawal in BSNL Dataone on Jul 31, 2010 at 09:30:44

need suggestions about buying mp3 player
Last post by pradeep in General Hardware Discussions on Jul 30, 2010 at 06:44:14

meghbela broadband
Last post by partha_777 in Other DSL Broaband Services on Jul 28, 2010 at 13:07:40

I get Error:691 invalid username and password
Last post by narayanasparta in BSNL Dataone on Jul 28, 2010 at 05:23:13


Uncapping bsnl limit?
Last post by din4204u in BSNL DataOne: Hardware on Jul 27, 2010 at 15:30:39

Olive Pad VT100 - Indian Android tablet
Last post by pradeep in General Hardware Discussions on Jul 26, 2010 at 04:33:30

does anyone know where to get a black rose?
Last post by pitamm in The Lounge on Jul 25, 2010 at 04:32:08

Internet connection problem - it only connects in battery
Last post by pitamm in BSNL DataOne: Plans / Services / Billing on Jul 25, 2010 at 04:29:46

Slooowww connection...
Last post by d11dmb in BSNL Dataone on Jul 24, 2010 at 09:03:36

BSNL Broadband Users
Last post by raviagrawal in BSNL Dataone on Jul 23, 2010 at 14:10:28




All logos and trademarks and articles on this site are property of their respective owner. The comments are property of their posters, all the rest (c) 2000-2008 Vinu Thomas

 
Interactive software released under GNU GPL, Code Credits, Privacy Policy