Creating a Simple Weblog in PHP and MySQL
By Michael Moncur (October 10, 2003)
Weblogs are one of the most popular ways of publishing on the Web. While you can use one of the many weblog services, it's also simple to program a weblog yourself. This is a good place to start if you want to add custom features or integrate a weblog into your site.
This tutorial uses PHP (version 3 or 4) and MySQL (version 3.2 or later) to create a minimal but functional weblog. To make sense of it, you should have a bit of experience with both MySQL and PHP.
Creating the Table
To begin, you will need to create the MySQL table to store weblog entries. This example will use a table with the following columns:
To create the table, you can use the following MySQL command. Enter it using the MySQL command-line interface or a front-end such as PHPmyAdmin. Before typing this command, type a USE command to select the database that you will be using.
CREATE TABLE weblog (
Creating the Add Entry Page
The weblog will use two PHP files: one to display the weblog to visitors, and another to add an entry. The addentry.php file will include the HTML to display a form for you to type an entry as well as the PHP code to store the entry in the database.
The HTML Form
Since this is a very simple weblog, we only need two fields in the form: a text field for the title of an entry, and a text area for the entry itself. You don't need to prompt for the date, since MySQL will assign it automatically. Here is the HTML for the page before adding PHP:
The PHP Code
Now all you need is some PHP code to detect when the Submit button is clicked and save the entry in the database. Add the following after the <BODY> tag:
This uses the mysql_connect and mysql_select_db functions to connect to the database. Be sure to specify your server, username, password, and database name. It then constructs a MySQL query in the $query variable, using the data entered into the form, and uses the mysql_query function to insert the data.
Testing the Add Entry Page
Now that you've completed the HTML and PHP, you should have the complete addentry.php file.
NOTE: This program does not implement any kind of security. You should place addentry.php in a directory that uses HTTP authentication or another method to ensure that unauthorized users cannot add entries.
Creating the Weblog Page
The other major component of the weblog is a PHP program to display the latest entries from the database. This will be the page visitors access to view your weblog. The following is the complete HTML and PHP code for weblog.php:
How it Works
The above code includes a simple HTML wrapper and PHP code that includes the following:
Testing the Weblog Page
To test weblog.php, you should already have added at least one entry using addentry.php. You can copy the code for this file from the listing above or download it. When you load this page, it should display your most recent additions to the weblog.
Taking it Further
Of course, this is just a simple illustration of how a weblog based on PHP and MySQL works. It would work fine as a mini-weblog to display the latest news about your site. To build a more complete weblog you may wish to do the following:
The author's weblog at www.figby.com began with a simple PHP and MySQL example like this one. It has been further customized with lots of PHP, CSS, and HTML but the fundamental structure remains very similar.
This is a simple example of how powerful MySQL and PHP can be: once you've done the programming, you have a site that you can update by filling out a simple form, without messing with HTML or PHP code. While commercial and open-source weblogs are much more feature-rich, I hope this simple example has given you an idea of how easy it is to develop a custom site from scratch.
(c) 2003-2005 by Michael Moncur. All rights reserved. No content may be reproduced without explicit consent.