Creating A GuestBook
Part 1: Creating the database table
This tutorial will run through creating a very simple PHP and MySQL based guestbook, from creating the database table to storing and displaying the messages and assumes that you have either phpmyadmin or some other database manager available. phpmyadmin can be downloaded from www.phpmyadmin.net
Structure
When you create a table in MySQL you need to decide what information you're going to store there and how many columns you'll need. For a simple guestbook you would probally want a column for each of the following: name, email, message, date posted.
The first thing you need is a CREATE TABLE statement, then you also need to give the table a name, we'll call it 'guestbook'
Now it's useful to give each of the guestbook entries an id number, so that later we can refer to the id if we want to edit or delete certain posts, so the next line for creating our table could be something like: id INT(10) not null AUTO_INCREMENT
This is basically creating a field of integer type with a width of 10 and the 'auto_increment' will mean each entry has a unique id making them easier to refer to later.
Now onto the fields for the entries into the guestbook:
- Name - the person posting the message, we'll use
name VARCHAR(50)this gives us a field that can have a varying number of characters with a maximum length of 50. - Email - the email of the person posting the message
email VARCHAR(50)same as above. - Message - the actual message the person is posting, for this we'll use
message TEXTa text field as we don't want the same restrictions on the message as we have on the name and email. - Date - the time and date the message was posted
date CHAR(10)we'll be using a UNIX time value which is 10 characters in length. (It'll be 11 characters in about 35 years time, so i don't think we really need to allow for that =])
Finally, we'll add the following PRIMARY KEY(id) a primary key is a unique key where each entry must be defined as not null, this is the main key that MySQL will use when refering to entries in the table.
Put It Together
Now that we have everything ready to create the table we need, all that's left is to put it together:
CREATE TABLE guestbook
(
id INT(10) not null AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(50),
message TEXT,
date CHAR(10),
PRIMARY KEY(id)
);
And you're done, use the above code to create a new table in your database and now you have everything ready to start adding entries so i guess you'll now want to move on and read part two.


