对单独的评论区域进行编码的有效方法是什么?

发布于 2024-09-11 17:00:57 字数 96 浏览 8 评论 0原文

我意识到这是一个抽象的问题,有几个答案,但我不知道从哪里开始。我想为我的每篇博客文章都有一个单独的评论区。每次更新代码以包含最新条目时,是否应该手动为每个条目的注释设置不同的表?

I realize this is somewhat of an abstract question that has several answers, but I am at a loss as to where to begin. I want to have a separate comments area for each of my blog posts. Should I just set up a different table for the comments for each entry manually every time I update the code to include the latest entry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小瓶盖 2024-09-18 17:00:57

为评论创建一个新表,其结构类似于(当然您可以根据需要自定义它):


Comments
    id INT NOT NULL auto_increment,
    blog_id INT NOT NULL,
    author_id INT NOT NULL DEFAULT 0,
    comment text NOT NULL,
    added_date DATETIME NOT NULL

author_id 链接到登录用户的用户表,0 表示匿名用户。我希望其他一切都应该是不言自明的。

Create a new table for the comments with a structure similar to (of course you can customize it to your needs):


Comments
    id INT NOT NULL auto_increment,
    blog_id INT NOT NULL,
    author_id INT NOT NULL DEFAULT 0,
    comment text NOT NULL,
    added_date DATETIME NOT NULL

The author_id is linked to the users table for logged in users, 0 for an anonymous user. Everything else should be self explanatory I hope.

2024-09-18 17:00:57

我不确定你的意思是什么......但听起来你想针对每篇文章发表评论。如果是这种情况,只需在评论表中为“post_id”或类似的内容创建一个字段即可。然后在每个帖子页面上只需使用 SELECT 语句来获取该特定 post_id 的评论。

I'm not sure what you're quite getting at... but it sounds like you want to have comments specific to each post. If that's the case, just simply make a field in the comments table for "post_id" or something similar. Then on each post page just use a SELECT statement to grab comments for that particular post_id.

天涯沦落人 2024-09-18 17:00:57

只需使用一个数据库表将帖子 ID 存储在其中一个字段中即可。类似于以下内容:

CREATE TABLE blog_comments (
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  author_id INT(10) UNSIGNED NOT NULL DEFAULT '0',
  post_id INT(10) UNSIGNED NOT NULL,
  comment TEXT NOT NULL,
  added_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);

然后您可以简单地查询帖子评论,如下所示:

$pid = 13; // or whatever your post ID is; could be a $_GET value
$sql = "SELECT * FROM comments WHERE post_id = '$pid' ORDER BY added_on DESC";

当然,请务必清理上述查询,因为您不希望任何人传递他们对 $pid 的值的感觉。您需要确保它是一个数字,并且只是一个数字。

Simply have a database table storing the post ID in one of the fields. Something similar to the following:

CREATE TABLE blog_comments (
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  author_id INT(10) UNSIGNED NOT NULL DEFAULT '0',
  post_id INT(10) UNSIGNED NOT NULL,
  comment TEXT NOT NULL,
  added_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);

Then you can simply query for post comments like thus:

$pid = 13; // or whatever your post ID is; could be a $_GET value
$sql = "SELECT * FROM comments WHERE post_id = '$pid' ORDER BY added_on DESC";

Of course, be sure to sanitize the above query, as you don't want any one passing what they feel like for the value of $pid. You need to make sure it's a number, and a number only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文