Do you want to encourage your visitors to say hi when they visit your friendly Website? This tutorial is divided in 4 phases and will teach you how to create a simple PHP guestbook using only one PHP file
We will create the PHP/MySQL Guestbook in four phases
- Setting up the database
- Creating the Guestbook form
- Coding the PHP script that will process the guestbook submissions
- Coding the PHP script that will display the guestbook entries
1. Setting up the database
The table's name will be guestbook. We are going to setup the following columns:
- id - the unique identifier for a guestbook entry
- name - the visitor's name
- email - the visitor's email
- comments - the message
- datetimecreated - to record the date and time the visitor submitted an entry
If you haven't created a database yet, using whatever interface you use for working with MySQL databases, you can execute the following command to create a database.
You can execute the following command to create our guestbook table.
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name TEXT,
email TEXT,
comments TEXT,
datetimecreated DATETIME NOT NULL
);
2. Creating the Guestbook form
<input type="text" name="name" />
<input type="text" name="email" />
<textarea name="comments" rows="10" cols="50" >
</textarea>
<input type="hidden" name="process" value="1" />
<input type="submit" value="Send Message!"
</form>
We have 3 fields where the visitor can enter data and 1 hidden field with a preset value:
- name: for the user to input his/her name
- email: for the user to input his/her email address
- comments: guestbook comments
- process: is a hidden fields that will send a value of 1 when the user clicks the Send Message! button. We use this hidden field to let the PHP code know that the user has clicked on the button and that it should process the code that inserts the data in the database
When the user clicks the Send Message! button, a $_POST array is created. $_POST is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form that uses the POST method.
3. Coding the PHP script that will process the guestbook submissions
{
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$query = 'INSERT INTO guestbook SET
name = "'.$name.'",
email = "'.$email.'",
comments = "'.$comments.'",
datetimecreated = NOW()';
if(mysql_query($query))
{
echo '<p style="color:#00F;">Thank you for signing our guestbook!</p>';
}
else
{
echo '<p style="color:#C00;">Sorry, your entry could not be submitted.</p>';
}
}
if(isset($_POST['process']) and $_POST['process']==1) allows us to process the code inside the if statement ONLY when the submit button has been clicked.
We build the query using the values entered into the form and the NOW() MySQL function which returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' format.
The msyql_query() function returns TRUE if the email was successfully sent, FALSE otherwise. If the function was executed successfully, a thank you message in blue letters will be displayed. If the function returns a FALSE value (the query was not successful), the code echos an error message in red letters.
4. Coding the PHP script that will display the guestbook entries
$result = mysql_query($query);
if(!$result)
{
echo '<p style="color:#C00;">Could not retrieve guestbook entries.</p>';
}
else
{
while($row = mysql_fetch_array($result))
{
echo '<p>';
echo '<strong>'.$row['name'].'<strong><br />';
echo $row['email'].'<br />';
echo date('F j, Y', strtotime($row['datetimecreated'])).' at '.date('g:i a', strtotime($row['datetimecreated'])).'<br />';
echo $row['comments'];
echo '</p>';
}
}
?>
The query will sort the results from newest to oldest by including the ORDER BY clause.
discuss this topic to forum
