Designing the Database Tables

Think of the basic components of a forum: topics and posts. A forumif properly used by its patronsshould 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 may 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 it was created, and the person creating 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)

By the Way

In this simple forum example, we will identify users by their email addresses and not require any sort of login sequence.

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.

Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: