SEARCH
  Web Programming PHP Making guestbook

Making guestbook

Joseph Skidmore PHP Aug 01, 2006

Making GuestbookIn this tutorial I will show you how to create a nice simple guestbook, I will be using PHP and MySQL for this.

The first thing we are going to need to do is determine what data we want to store, for a guestbook we will most likely want to store the users name, email address, the date and their comments.

To start, create a database called 'guestbook' (always keep database names relevant) and then insert this query:

CREATE TABLE guestbook (
ID int NOT NULL AUTO_INCREMENT,
name varchar(30),
email varchar(60),
dateposted date,
comment text,
INDEX (ID)
);

Here is the query, we are creating a table called guestbook with 5 fields.

The next thing we are going to need to create is the form in which users will input their data.

signguestbook.php

<form name="signguestbook" method="POST" action="addmessage.php">
<table cellspacing="4" style="padding: 0px; width: 500px;
border: 0px;">
<tr>
<td align="right"><b>Name:</b></td><td><input type="text" name="name" /></td>
</tr>

<tr>
<td align="right"><b>EMail:</b></td><td><input type="text" name="email" /></td>
</tr>

<tr>
<td colspan="2" align="center"><b>Comment:</b><br />
<textarea name="comment" rows="5" cols="50"></textarea></td>
</tr>

<tr>
<td colspan="2" align="center"><input type="reset" name="reset" value="RESET" />
<input type="submit" name="submit" value="SUBMIT" /></td>
</tr>
</table>
</form>

Now our form is set to POST and forwards all the data to addmessage.php which is where all the storing input to the database will happen.

addmessage.php

<?php
function safeAddSlashes($string) {
if (get_magic_quotes_gpc()) {
return $string;
} else {
return addslashes($string);
}
}

function mailcheck($emailadr){
if (eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$",
$emailadr))
return $emailadr;
else
return false;
}

$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$datetime = date("Y-m-d");

$comment = safeAddSlashes($comment);
$email = mailcheck($email);

$dbHost = "localhost";
$dbUser = "";
$dbPass = "";
$dbname = "guestbook";

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);

$sql="INSERT INTO guestbook ('name', 'email', 'dateposted', 'comment')
VALUES ('$name', '$email', '$datetime', '$comment')";

$result = mysql_query($sql, $db);

if ($result) {
echo 'Thank you, your message has been entered successfully<br />';
echo 'To view the guestbook click <a href="/img_articles/9106/viewguestbook.php"
title="View Guestbook">here</a>.';
} else
echo 'Your message could not be added.';
?>

The first strange thing you may notice with this code is the JavaScript function at the top, well I will explain to you what it does:

This function is for protection

function safeAddSlashes($string) {
if (get_magic_quotes_gpc()) {
return $string;
} else {
return addslashes($string);
}
}

This is the function to check if the users email address is valid, is splits up the email address into 2 parts, the text before the @ and the bit after it to make sure a valid email is entered, if it is valid it will be sent along, if it is not then a NULL value will be sent.

function mailcheck($emailadr){
if (eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$",
$emailadr))
return $emailadr;
else
return false;
}

The next step is creating the output of the guestbook, since this is only a tutorial I won't make it look spectacular, I will leave it basic so you can edit it later on.

viewguestbook.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<title></title>

</head>

<body>

<h3>View Guestbook</h3>

<?php
$dbHost = "localhost";
$dbUser = "";
$dbPass = "";
$dbname = "guestbook";

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);


$requete = "SELECT ID, name, email, dateposted, comment
FROM guestbook ORDER BY ID desc";
$result = mysql_query ($requete,$db);

while($row = mysql_fetch_assoc($result))
{
$name = $row['name'];
$email = $row['email'];
$comment = $row['comment'];
$datetime = $row["dateposted"];

if ($email)
echo '<a href="mailto:'.$email.'">';
echo $name;
if ($email)
echo '</a>';
echo ' - Posted on '.$datetime.'<br />';
echo ''.$comment.'';
echo '<hr />';
}
?>

</body>
</html>

Now you will see in this code, when I am fetching the fields from the table I am calling them up in ORDER BY ID desc. When the data is sent to the database we have our fieldname ID give it a number, for the first ever record it would be 1, the next record would then be 2 and so on. When we call them in order by ID desc we call up the most recently added data first. Say 20 people have signed the guestbook, number 20 would be the first shown record, then 19, 18, 17 ect ect.

Now with the bare bones of the this code you should be able to implement it into your site and make it a lot better to look at. If you would like to download the pre-written guestbook script you can do so here: Pre Written Guestbook Script

subscribe to newsletter