10.1. Creating a comments Table in the Database

Before you can begin working with comments, you need to have a place to store them. Create a table called comments in the simple_blog database; you'll use this to store all of your comment information.

You need to store several different kinds of information in this table:

  • Id: A unique identifier for the comment. This is the table's primary key. You can use the AUTO_INCREMENT property to simplify adding new comments.

  • blog_id: The identifier of the blog entry to which the comment corresponds. This column is an INT value. To speed up your queries, you can also create an index on this column.

  • name: The name of the commenter. This column accepts a maximum of 75 characters and is of the VARCHAR type.

  • email: The email address of the commenter. This column accepts a maximum of 150 characters and is of the VARCHAR type.

  • comment: The actual comment. This column is of the TEXT type.

  • date: The date the comment was posted. You set this column to store the CURRENT_TIMESTAMP when a user adds a comment i to the table, which means it is of the TIMESTAMP type.

To create this table, navigate to http://localhost/phpmyadmin in a browser and open the SQL tab. Executing the following command creates the comments table and prepares it for use:

CREATE TABLE simple_blog.comments
(
id INT PRIMARY KEY AUTO_INCREMENT,
blog_id INT,
name VARCHAR(75),
email VARCHAR(150),
comment TEXT,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX(blog_id)
)

Once you click the Go button, you can click the comments table in the simple_blog database to see the structure of your table (see Figure 10-1).

Figure 10.1. The comments table as viewed in phpMyAdmin

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.179