Coding the Guestbook in PHP
This page covers:
Start with Pseudocode
Connect to the DB
Check for Form Submission
Get Existing Guestbook Entries
Write the Web Page
The Finished Guestbook
Start with Pseudocode
I always start by jotting down some quick logic with either
pseudocode
or a flow chart. For this example, we need to connect to the database. If the form has been submitted,
insert into the db. Query the database for existing guestbook entries and print them. Print the form
for users to add new entries. Make sure to validate the form so the required fields are filled out (in
this case, only the comment field is required). Pseudocode would look something like this:
connect to db
if form submitted then
insert into db
if insert is successful
append success message to print_str
else
append failure message to print_str
query db for all entries
while recordset is not empty
get name, email, comment, date
if name is not empty
if email is not empty
link name to email address
else if email is not empty
link email to email address
else
name is empty
append comment, date, name to print_str
print print_str
print the form to add new comments
when form is submitted, validate that required field is not empty
Connect to the DB
So here we go. First we need to connect to the database because whether or not the form
is submitted, we're going to query it for the existing guestbook entries. I keep all my database
variables in my variable include file, and I have a database connection function that I normally call
to connect to the db, but I'll write it out here (don't forget to change the database variables for
your own database).
<?
$dbHost = "localhost";
$dbUser = "hardcoder";
$dbPass = "hardcoder";
$dbDatabase = "hardcoder";
$li = mysql_connect($dbHost, $dbUser, $dbPass) or die("Could not connect");
mysql_select_db($dbDatabase, $li) or die ("could not select DB");
?>
Check for Form Submission
Now we'll get into some of the meat of the code. This portion checks to see if the
form has been submitted, then inserts into the db if it has. Read the comments:
<?
$gb_str = "";
$pgeTitle = "View and Sign Guestbook";
if (!empty($HTTP_POST_VARS["submit"])) {
$name = $HTTP_POST_VARS["frmName"];
$email = $HTTP_POST_VARS["frmEmail"];
$comment = $HTTP_POST_VARS["frmComment"];
$date = Date("Y-m-d h:i:s");
$gb_query = "insert into guestbook
values(0, '$name', '$email', '$comment', '$date')";
mysql_query($gb_query);
$res = mysql_affected_rows();
if($res > 0) {
$ret_str="Your guestbook entry was successfully added.";
} else {
$ret_str = "Your guestbook entry was NOT successfully added.";
}
$gb_str .= "<span class=\"ret\">$ret_str</span><BR>";
}
?>
Get Existing Guestbook Entries
Now we need to query the database for existing guestbook entries. We do this regardless of
whether or not the form has been submitted.
<?
$get_query = "select gbName, gbEmail, gbComment, DATE_FORMAT(gbDateAdded, '%m-%d-%y %H:%i') gbDateAdded
from guestbook";
$get_rs = mysql_query($get_query);
$gb_str .= "<hr size=\"1\">";
while($get_row = mysql_fetch_array($get_rs)) {
$name = $get_row["gbName"];
$email = $get_row["gbEmail"];
$comment = $get_row["gbComment"];
$date = $get_row["gbDateAdded"];
if(!empty($name)) {
if(!empty($email)) {
$name="by <a href=\"mailto:$email\">$name</a>";
}
} elseif (!empty($email)) {
$name = "by <a href=\"mailto:$email\">$email</a>";
} else {
$name = "";
}
$gb_str .= "<br>$comment<p class=\"small\">< posted on $date $name><hr size=\"1\">";
}
mysql_free_result($get_rs);
?>
Write the Web Page
Now we display the guestbook entries and the form to add new entries on the page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Guestbook</TITLE>
<SCRIPT language="javascript">
<!--
function stripCharsInBag (s, bag) {
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function valForm(frm) {
badChars = "<[]>{}";
if(frm.frmComment.value == "") {
alert("Please fill in your comments for the guestbook.");
return false;
} else {
frm.frmComment.value = stripCharsInBag(frm.frmComment.value, badChars);
frm.frmName.value = stripCharsInBag(frm.frmName.value, badChars);
frm.frmEmail.value = stripCharsInBag(frm.frmEmail.value, badChars);
return true;
}
}
-->
</SCRIPT>
</HEAD>
<BODY bgcolor="#FFFFFF">
<? echo $gb_str; ?>
<form name="gb" action="<? echo $PHP_SELF;?>" method="post">
<table cellpadding="3" cellspacing="0" border="0">
<tr>
<td class="tdhead" valign="top" align="right">Name</td>
<td valign="top"><input type="text" name="frmName" value="" size="30" maxlength="50"></td>
</tr>
<tr>
<td class="tdhead" valign="top" align="right">Email</td>
<td valign="top"><input type="text" name="frmEmail" value="" size="30" maxlength="100"></td>
</tr>
<tr>
<td class="tdhead" valign="top" align="right">Comment</td>
<td valign="top"><textarea name="frmComment" rows="5" cols="30"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="submit" onClick="return valForm(document.gb)">
<input type="reset" name="reset" value="reset"></td>
</tr>
</table>
</form>
</BODY>
</HTML>
<?
mysql_close($li);
?>
The Finished Guestbook
You can view the complete code for the guestbook here. I've
made some minor changes to it so I could use the site template, and you can view the working version
and test it out for yourself here.
|