joseph
joseph

Reputation: 41

MySQL Table for comments

I made a link to a page in a document named "find.php" and made $id equal to the id of an article. When you click on it, the url looks like find.php?id=w/e. I want to be able to post comments on the page. For instance if I wanted to post a comment on find.php?id=40, how would I display the comments? By the way, there's a table for the articles and a table for the comments.

For reference, I set up my comments table as

com_id int (11)
title text
user varchar (255)
msg text

Would I need a foreign key also?

Upvotes: 4

Views: 195

Answers (2)

Jean-Philippe Leclerc
Jean-Philippe Leclerc

Reputation: 6805

tableArticle:

id (int) PK
...
...

tableComment:

com_id (int) PK
article_id (int) FK to tableArticle on id
comment (varchar(255))

To display comments:

Your SQL query:

SELECT * FROM tableComment WHERE article_id = w\e id

Your code (note that I can't test the syntax now):

while($row=mysql_fetch_array($result)) 
{
     echo $row[1];
}

Upvotes: 2

Paul Sonier
Paul Sonier

Reputation: 39520

Yes, you will need a foreign key from the comments table to the articles table, so you can reference which article each comment is for.

Upvotes: 1

Related Questions