Coding a Simple Guestbook Using PHP and MySQL

    April 2, 2004

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.

We’ll need a form to prompt the visitor to leave a comment in your guestbook. Let’s call this page comments.php .

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.

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

The 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 simple 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>
<input type="submit" name="Submit" value="Submit">


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,

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
$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());

// 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
$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
$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());

// Display the top of the page for the guestbook
echo " <center><h1> A Simple Guestbook in PHP & MYSQL
echo " <a href="comments.php">Click here to Leave us a
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
guest_book 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 save the file as guestbook.php.

Now that you have all the components required for your guestbook, upload the three files: comments.php, guestbook.php and store_comment.php on your webserver. You can invoke your newly made guestbook by calling guestbook.php on your server. You can modify the code and format the pages to make the guestbook look good.

Vinu Thomas is a consultant on Webdesign and Internet Technologies. His website is You can discuss about this article or any PHP/MYSQL related issues in our Discussion Forums: