Creating an Online Storefront in PHP, MySQL and Apache, Part 1
If you're interested in seeing more projects like this, you can purchase Sams Teach Yourself PHP, MySQL and Apache All in One.
In this chapter, you’ll learn the design process behind a simple discussion forum. This includes developing the database tables and user input forms, and displaying the results. When broken into pieces like this, such a task seems simple—and it is! The ultimate goal is to understand the concepts and relationships that go into making something like a discussion forum, not to create the world’s most full-functioned system—in fact, you’ll see it’s sparse, but it sure is relational.
In this chapter, you will learn
How to create tables for a simple discussion forum
How to create input forms for a simple discussion forum
How to display a simple discussion forum
Designing the Database Tables
Think of the basic components of a forum: topics and posts. A forum—if properly used by its patrons—should have several topics, and each of those topics will have one or more posts submitted by users. Knowing that, you should realize that the posts are tied to the topics through a key field. This key forms the relationship between the two tables.
Think about the requirements for the topics themselves. You definitely need a field for the title, and subsequently you might want fields to hold the creation time and the identification of the user who created the topic. Similarly, think of the requirements for the posts: You’ll want to store the text of the post, the time of its creation, and the identity of the person that created it. Most importantly, you need that key to tie the post to the topic.
The following two table creation statements create these tables, called forum_topics and forum_posts:
mysql> CREATE TABLE forum_topics ( -> topic_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -> topic_title VARCHAR (150), -> topic_create_time DATETIME, -> topic_owner VARCHAR (150) -> ); Query OK, 0 rows affected (0.03 sec) mysql> CREATE TABLE forum_posts ( -> post_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -> topic_id INT NOT NULL, -> post_text TEXT, -> post_create_time DATETIME, -> post_owner VARCHAR (150) -> ); Query OK, 0 rows affected (0.00 sec)
You should now have two empty tables, waiting for some input. In the next section, you’ll create the input forms for adding a topic and a post.