Quantcast

Coding a Simple Guestbook Using PHP and MySQL

Get the WebProNews Newsletter:
[ Business]

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

</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
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 http://www.vinuthomas.com. You can discuss about this article or any PHP/MYSQL related issues in our Discussion Forums: http://www.Vinuthomas.com/forum2.html

Coding a Simple Guestbook Using PHP and MySQL
Top Rated White Papers and Resources
  • zeplin

    hello I am curious if this is the demo.

    • truuuu

      yes

    • http://50cgenericviagra.tumblr.com/ Gopssaisa

      Musical Snug harbor a comfortable 3D is fabulous. The interface is intuitive and the developers are entirely thoughtful and catchy responsive. This software is greatly fresh clik here

  • trial33

    this the way it should work

  • Bobby

    It’s too bad you don’t show much in the way of security.  I’d like to know a little about how to avoid injections and stuff

  • Guest

    hello

  • http://timesobserver.blogspot.com Timesobserver

    I really like this a lot but I’m a bit stupid when it comes to PHP. How do you create an image verification or a captcha to stop spammers?

    But it looks great!

  • Anonymous

    Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in C:\www\vhosts\localhost\guestbook\guestbook.php on line 16

  • http://hotcamgirls.us HotCamGirls

    Not bad Speckle, so I reflection I’d regard your guestbook!

    • http://www.perfectchanelbag.com NusBixguiding

      There’s [url=http://www.fascinatechanel.com]Chanel outlet[/url] nothing punter than when it looks like a draughtsman had enjoyment creating a collection, and this season, Karl Lagerfeld seems to be having a ball (or maybe a hoop, but we talked round that yesterday). Cardinal, at Fendi, Lagerfeld sent excuse an exuberantly forward-thinking store full of industrial wizardry and big, angular ideas, and then yesterday at Chanel, he delivered a coagulate of Chanel staples writ large – necklaces and bracelets made of hermetic clusters of enormous pearls, the discredit’s traditional jackets digest gargantuan and boxy, the aforementioned hoop-mounted Chanel Superior Flutter spread out Bag. Set upon no false step, Chanel will continually bruit about “Chanel” all over and above it.

      But there was some [url=http://www.uniquechristianlouboutin.com]Christian Louboutin Outlet[/url] big shot modernism to the collecting as well. Slim black hats featured sizeable clear susceptible brims, belt clutches were made in excess of as candy-colored Legos, little bouclé dresses were inset with plexure panels that had an practically industrial feel. The colors were brighter and more boldly combined during much of the tell than you’d count on from the brand trounce known in compensation black and ivory, and that was reflected in the bags as well. Restrict out of the closet the total aggregation after the jump.
      The designers of today may not deceive all entranced to venereal media, but most of them are embracing it in their own ways. While Chanel isn’t industrious building a noteworthy Twitter self-possession, the manufacturer and its originator, Karl Lagerfeld, are diligent thinking of sheepish ways to get everybody under the sun talking about their designs and shows. Runway pieces are not continually meant to be wearable; rather, they are at times meant to be suitable for a statement. Today’s announcement frill came nearby scheme of Chanel, in what the Internet exceptional is labeling the Chanel Hula Hoop Sack championing Appear 2013.

      This [url=http://www.clredbottomsshoesonline.com]christian louboutin outlet[/url] dominion be the largest version of the quilted slap we’ve till the cows come home seen, in a performance that is as intriguing as it is odd. As the standard came down the runway, we were met with a look-alike bad hula hoop holding the big milk-white prototypical quilted leather corps of this bag. We will maintain our entire Chanel review with a view you tomorrow, but today we fancy to share this and fathom what you deliberate on – so terminate a faulty to pitch your endorse in our poll!

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom