Sam Gabriel
Sam Gabriel

Reputation: 327

How to add comments section to php website

Hey guys, I have a website that has a url that looks sorta like this: www.domain.com/photos.php?id=(image id); Now, my question is, how can I add comments to each picture using mysql and php.

Upvotes: 0

Views: 1658

Answers (1)

kilrizzy
kilrizzy

Reputation: 2943

You will probably want a new table to link the user with the comment with the picture:

table: comments

id (comment id), user (user id), picture (pic id), comment (text), date(timestamp w/default current time)

Then after you show the image, do another query for any comments:

$comments_query = mysql_query("SELECT comment FROM comments WHERE picture = $id");
while($comments_result = mysql_fetch_array($comments_query)){
   echo($comments_result['comment']);
}

You will also probably want to link each user's username to the comment as well:

$comments_query = mysql_query("SELECT comments.comment, users.username, comments.date FROM comments INNER JOIN users ON comments.user = users.id WHERE comments.picture = $id");
while($comments_result = mysql_fetch_array($comments_query)){
    echo($comments_result['date']);
    echo($comments_result['username']);       
    echo($comments_result['comment']);
}

Upvotes: 3

Related Questions