Lead Consultant: Stefan Christian Densmore
(513) 379-8992 or email


iWebspider.com
Website Management, PHP/MySQL Development, Hosting & Design.

PHP: Simple Guestbook Tutorial

February 23rd, 2009 – 8:33 pm
Tagged as: Articles, Tutorials
This tutorial was originally posted at 
The purpose of this tutorial is to show you how to create a very simple guestbook using PHP and a MySQL database. So lets get started:

Creating a database:

First thing we need to do is to create our mysql database. You can use the code below to create yours or use this as a example to create yours manually.

Create a database named guestbook_db then create this table.

CREATE TABLE `guestbook` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 50 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
`message` TEXT NOT NULL ,
`time` TIMESTAMP( 14 ) NOT NULL
) ENGINE = MYISAM ;

Or in PHP code:

PHP Code Example:
<?
$sql
= 'CREATE TABLE `guestbook` ('
. ' `id` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, '
. ' `name` VARCHAR(50) NOT NULL, '
. ' `email` VARCHAR(50) NOT NULL, '
. ' `message` TEXT NOT NULL, '
. ' `time` TIMESTAMP(14) NOT NULL'
. ' )'
. ' ENGINE = myisam;';
?>

On to the good stuff

Now we can start writing our code… see the comments in the code for instructions and who and whats is what.

PHP Code Example:

<?
include "database_connection.php"; // Include the database information
?>
<HTML>
<HEAD>
<TITLE>Guestbook</TITLE>
</HEAD>

<BODY>
<?
if(isset($_POST['submit_entry'])) {
// If a post is being submitted then do the following
dbConnect("guestbook_db"); // Connect to database

/* This following code will loop through all the information the user sent using the form
and strip out harmful text, code and html.
After this code is execute it puts the filtered values back into the original variables */
for(reset($HTTP_POST_VARS);
$key=key($HTTP_POST_VARS);
next($HTTP_POST_VARS)) {
$this = addslashes($HTTP_POST_VARS[$key]);
$this = str_replace(array("", "/"), '', $this);
$this = strtr($this, ">", " ");
$this = strtr($this, "<", " ");
$this = strtr($this, "|", " ");
$key = $this;
}
// This will stop a user from submitting a empty form
if ($name && $email && $message ) {
// Now we insert the data from the form into our database table
$sql = "INSERT INTO guestbook SET
name = '$name',
email = '$email',
message = '$message',
time = NULL";
// If there is a problem return an error
if (!mysql_query($sql))
error('A database error occurred in processing your '.
'submission.nIf this error persists, please '.
'contact admin@yourdomain.com.');
// Return this message to the user if everything was successful
echo "Message added to guestbook";
}
else {
echo "Please fill out all the fields in the form";
}
exit;
}
?>

<H1>Entries</H1>
<?
dbConnect("guestbook_db"); // Connect to database

$limit = 10;
// Select all of the entries in the table guestbook and order then by their ID
$sql = "SELECT * FROM guestbook ORDER BY id DESC";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

// Count the number of rows(entries) in our table(guestbook)
$num=mysql_numrows($result);
// Set our counter to 0
$i=0;
// Create a while loop that will go through all of our entries in the table
while ($i < $num) {
// Putting our entries from the table into short variable names
$name=mysql_result($result,$i,"name");
$email=mysql_result($result,$i,"email");
$message=mysql_result($result,$i,"message");
$time=mysql_result($result,$i,"time");
// This is where we start our html code for listing the entries in our database
?>

<b>Name:</b> <? echo $name ?>
<br><b>Email:</b> <? echo $email ?>
<br><b>Date/Time:</b> <? echo $time ?>
<br><b>Message:</b> <? echo $message ?>
<HR>

<?
$i++; // Increment our counter for the next entry in the guestbook
}

?>


<H1>Add A Message</H1>
<FORM METHOD="post" ACTION="#">
<PRE>
Your Name:       <INPUT
TYPE="text"
NAME="name"
SIZE="20"
MAXLENGTH="50">
Your Email:      <INPUT
TYPE="text"
NAME="email"
SIZE="20"
MAXLENGTH="50">

Enter Message:
<TEXTAREA NAME="message" COLS="40" ROWS="8" WRAP="Virtual">
</TEXTAREA>

<INPUT TYPE="submit" NAME="submit_entry" VALUE="Add">

</PRE>
</FORM>
</BODY>
</HTML>


Text to display before author profile  Located in East Walnut Hills, Cincinnati, Ohio, iWebspider Incorporated is owned and operated by Stefan Christian Densmore, providing open source information technology solutions since 2003. Read more from this author


No Comments

» Leave a comment now

» RSS feed for comments on this post
» TrackBack URI

No comments yet.


Leave a Comment

  1. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This site is using OpenAvatar based on



Peace, Love, and Technological Superiority