Analysis

The goal of a guestbook is to capture a message from your visitors, as well as their name and email address, then display all that to visitors who can read your guestbook. Therefore, the most basic guestbook is going to look roughly like this:

GuestName

CHAR(255)

GuestEmail

CHAR(255)

GuestMessage

TEXT

DateSubmitted

INT

Naturally we will have an auto-incrementing ID primary key in there too, to enable us to work with individual records easily.

To handle both posting and reading messages, we will need two scripts: post.php and read.php. There are two ways we can choose when posting messages - we can use post.php to display the input form and handle saving it to our database and have it show a "Thanks for posting" message, or we can use read.php to handle saving the data to our database, which means users would immediately see their message live in the guestbook.

Which you choose to implement generally depends on your philosophy, and should match the behaviour seen elsewhere in your site. Webmasters who want every possible advert displayed will generally opt for the "Thanks for posting" scheme, as it generates one more page hit per use. Webmasters who are trying to conserve bandwidth or who do not care about adverts being displayed will generally opt to display the guestbook with the new post in there - as I said, it all depends on your philosophy.

It is fairly easy for you to switch between the two choices, so we will be going along with the "Thanks for posting" plan.

To handle posting to our guestbook we need post.php to have a name box, an email box, and a message text area. This is then submitted and entered directly into our database, and a thank you message is sent back along with a link to read.php. All clear? Let's get programming...

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Development >>

Previous chapter: Creating a guestbook

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.