php - 如何给php网站添加评论区

标签 php mysql comments

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

最佳答案

您可能需要一个新表来将用户与评论与图片链接起来:

表格:评论

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

关于php - 如何给php网站添加评论区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4571552/

相关文章:

php - PHP中如何根据 session 数据将用户重定向到不同的页面?

PHP 不再添加到数据库

css - 这是什么意思/*!一些 url 或文本 */in css

php - 如何删除评论中的电子邮件和网站 ((Wordpress))

view - web2py 服务器端注释

php - 如何使用php和mysql从外表中搜索数据?

javascript - HTML 表单 POST 到直接打印页面

php - Piwik 升级到 MySQL 5.7+ SQLSTATE[HY000] : General error: 2006 MySQL server has gone away - caused by plugin Actions

sql - MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET

Debian 上的 MYSQL : what's the real host name?