Setting Up the Database
The blog database is very straightforward. You will have two tables, one for the entries, and one for the categories of those entries. These are set up using the PhpMyAdmin tool from the Uniform Server. If you haven't done so already, go to the administration screen (Figure 1
) and start the MySQL database by clicking 'Run MySQL Server'. Once it has started, you will be directed to a confirmation screen. Click 'Back' on this screen to go back to the administration screen. Once there, click the PhpMyAdmin link to start playing with the database.
You should see a screen like that shown in Figure 2
In the 'Create new database' section, enter the name 'Blog' and click 'Create'. You will be taken to the screen shown in Figure 3.
Figure 2. phpMyAdmin Home Screen: phpMyAdmin is the facility you use to set up your blog's back end data store.
Figure 3. The Two Tables: The screen will allow you to create a table on your database.
|Figure 4. Categorization: This screen is where you begin to name the fields in your database tables. |
To create the categories table, enter 'Categories' in the 'Name' field, and '2' in the 'Fields' field. This will create a simple table with 2 fields. One field will be used as a numeric ID for the category, the other as a textual description of the category. Once you have done that, click the top 'Go' button (beside the 'Fields' entry). You will then be taken to a form where you can describe the database fields (see Figure 4
Enter the fields as shown.
Field 'ID' should be an INT that is 'not null' and its 'Extra' setting set to 'auto_increment'. It should also be a unique value.
Field 'Description' should be a VARCHAR with a length of 100.
When you are done, click 'Save'. You now have a table on your database called 'Categories.' Repeat the above steps to create a table called 'Blogs' with the following fields.
Field 'ID' should be an INT that is 'not null' and its 'Extra' setting set to 'auto_increment'. It should be a unique value.
Field 'Category' should be an INT that is 'not null.'
Field 'Headline' should be a 'VARCHAR' of length 100 that is not null.
Field 'Body' should be a 'BLOB.' This is because a VARCHAR can only hold up to 255 characters, which is not enough for most entries.
Entering Information to the Database
From the home screen of phpMyAdmin (Figure 2
), select the 'blog' database from the drop down on the left-hand side. This will take you to the administration screen for the database (see Figure 5
Figure 5. Behind the Curtain: Getting to the Administration screen for the blog database is simple.
Figure 6. Choose Your Categories: Enter the names of your blog categories into the database using this form.
To enter new information on a table, use the 'insert' icon under the 'Action' heading. If you hover your mouse over the icons within this section you will see tooltips describing them. Select the 'insert' icon on the 'categories' row. You will be taken to the screen in Figure 6
In the 'Description' field, enter the value 'Movies' and click 'Go'. This will make a category entry for movies. You do not need to do anything in the ID field as it is set to 'auto_increment' meaning that the database will handle it for you. 'Movies' is now associated with the ID '1'. Repeat these steps to make more entries for whatever categories you like.
Building the Data Entry Pages
Data entry is achieved using two pages. The first is an HTML form that provides the user interface, the second a PHP script that processes the input from the form. Using an HTML designer, such as Dreamweaver or FrontPage, a simple form such as the one defined by the code in Listing 1 can be developed. The full page is available in the download accompanying this article.
This form is very skeletal and simple and can be designed or updated with any HTML designer applicationor if you prefer by tweaking the code. The only things you will need to keep note of if you are making your own are the control names. The processing script expects the names to be tHeadline, tBody, and tPin, respectively.
In this simple case, you aren't using the categories, but as the categories are taken from the database at runtime, you need to get a little more fancy. In the next section you will see how to generate this HTML form on the fly with the up-to-date category list using PHP.