|
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:
<?
$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, I have a lot of comments in this code so if you have any questions just make a post in the forums.
<?
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>
|