MySQL table for comments

This is a very amateurish question (don't rate me for it), but how do I create a column

for comments

containing the identifier of the message to which they are assigned?

For example, if I posted a comment on post # 48, how would I install it in MySQL so that

Comment is displayed in message # 48, and not in all messages?

Thanks in advance:)

+3
source share
5 answers

From one to several relationships (one post can have many comments), so you will need a new table for it.

comments_tbl
 - comment_id  |   int(11) auto_increment
 - post_id     |   int(11) (FK to post table)
 - author_id   |   int(11) (FK to the user table OR author_name)
 - date        |   datetime
 - comment     |   text

And if you want to be able to tag and notice comments, you can include something like:

 - date_approved  |  datetime
 - flagged        |  int(1)

Then your SQL to display comments for the message will look like

mysql_query("SELECT comment_id, author_name, comment FROM comments_tbl WHERE post_id = '$postid' AND date_approved IS NOT NULL AND flagged = '0'");

:

mysql_query("INSERT INTO comments (post_id, author_id, date, comment) VALUES ('$postid', '$author_id', '$date', '$comment');
+2

, .

,

table Post
id,
content

table Comment
id,
content,
post_id

post_id - .

+6

PHP-, , (, $postId. new, ( , , ):

mysql_query("INSERT INTO comments (id_post, text) VALUES ('".$postId."', 'Text of the comment'");

, :

mysql_query("SELECT text FROM comments WHERE id_post = '".$postId."'");

, id_post ane, .

0

, . , post_id .

comments
- comment_id int(8) Auto_Increment 
- post_id int(8)
- comment_author varchar(255)
- comment_content text()

post_id , , . ID, , varchar(255) int(8) -, .

0

-

CREATE TABLE `comments` (id int unsigned not null auto_increment PRIMARY KEY,
post_id int unsigned not NULL,
// other fields, post_date, post_text, etc
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE ON UPDATE CASCADE)

: FOREIGN KEY INNODB, , MyISAM, . post_id , posts - , post.id .

0

All Articles