如何在php网站上添加评论部分

时间:2010-12-31 17:57:40

标签: php mysql comments

嘿伙计们,我有一个网站,其网址看起来像这样: www.domain.com/photos.php?id=(image id); 现在,我的问题是,如何使用mysql和php为每张图片添加注释。

1 个答案:

答案 0 :(得分:3)

您可能需要一个新表来将用户链接到带有图片的评论:

表:评论

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

然后在显示图像后,对任何注释执行另一个查询:

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

您也可能希望将每个用户的用户名链接到评论:

$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']);
}