PHP 注释代码帮助

发布于 2024-08-12 06:46:40 字数 450 浏览 5 评论 0原文

我正在编写我的第一个博客。在各种教程和其他论坛的帮助下,我成功地收集了半工作代码。

现在我有一个代码可以接受并显示评论,但问题是协调哪些评论出现在哪个帖子上。我当前的设置是我的所有帖子都是 HTML 文件,评论存储在数据库中。我还有一个表单,可以为每个帖子创建一个新行,其中包含唯一的帖子 ID 和标题。

我现在的基本数据库设置如下:1个数据库,2个表。帖子表和评论表。在评论表中,我有通用名称、网站、评论等,还有一个针对每个评论自动递增的唯一 ID。然后我有一个 post_id ,它应该与指定的帖子匹配。

在帖子表上,我只有两个字段:entry_id 和 title。标题是我手动设置的,entry_id是自动递增的。注意:条目本身不存储在数据库中。

所以我当前的问题是如何为每页评论设置post_id以及如何将entry_id与实际帖子关联起来。我希望这不会太令人困惑。非常感谢您的帮助!

-i大师

I'm in the process of coding my very first blog. With the help of various tutorials, and other forums I have managed to gather a semi-working code.

Right now I have a code that takes and displays the comment, but the problem is coordinating which comments go on which post. My current set up is all my post are HTML files, and the comments are stored in a database. I also have a form that creates a new row with a unique post ID and title for each post.

My basic DB setup right now is as follows: 1 database, 2 tables. A post table and a comments table. In the comments table I have the general name, website, comment, etc. and I also have a unique ID that auto-increments for each comment. Then I have a post_id which should match up with the designated post.

On the post table, I have just two fields: entry_id and title. The title is manually set by me and the entry_id is auto-incremented. NOTE: The entry itself is NOT stored in the database.

So my current problem is how to set the post_id for each page of comments and how to associate the entry_id with the actual post. I hope that's not too confusing. Thanks a ton for any help!

-iMaster

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

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

发布评论

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

评论(4

芯好空 2024-08-19 06:46:40

我认为您应该考虑重构代码以将帖子存储在数据库中。

从那里,您将有一个显示您的帖子的页面(http://mysite/showpost.php?post_id=5)(psuedo-code'ish):

<?php

// establish database connection here

// Simple SQL injection prevention:
foreach ($_REQUEST as $key => $value)
{
  $_REQUEST[$key] = mysql_real_escape_string($value);
}

// Get the appropriate post from the posts table.
$query = "SELECT post FROM posts WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);
$row = mysql_fetch_assoc($query);

echo $row['posts'];

// Get the appropriate comments from the comments table.
$query = "SELECT comment FROM comments WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
  echo "Comment: {$row['comment']}";
}

// close connections, etc.    

?>

我的 PHP 非常生疏,但是这应该可以让您很好地了解完成您想要的任务所需的数据结构和代码。

I think that you should consider refactoring your code to store the post in your database.

From there, you'd have a page (http://mysite/showpost.php?post_id=5) that displays your post (psuedo-code'ish):

<?php

// establish database connection here

// Simple SQL injection prevention:
foreach ($_REQUEST as $key => $value)
{
  $_REQUEST[$key] = mysql_real_escape_string($value);
}

// Get the appropriate post from the posts table.
$query = "SELECT post FROM posts WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);
$row = mysql_fetch_assoc($query);

echo $row['posts'];

// Get the appropriate comments from the comments table.
$query = "SELECT comment FROM comments WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
  echo "Comment: {$row['comment']}";
}

// close connections, etc.    

?>

My PHP is very rusty, but this should give you a good idea of the data structure and code needed to accomplish what you want.

心的位置 2024-08-19 06:46:40

对你学习“自己动手”并得到你想要的东西并一路学习一些东西很有好处。

您应该使用与文章 ID 匹配的外键创建评论表。

然后,在显示您的评论时,执行查询以获取与该文章 ID 关联的所有评论。

Good for you for learning to "roll your own" and get exactly what you want and learn something along the way.

You should create a table for comments with a foreign key that matches the article ID.

Then when displaying your comments, do a query to fetch all comments associated with that article ID.

黑色毁心梦 2024-08-19 06:46:40

您应该遵循 Ian 的建议并重构您的代码以使用表格。否则,当您创建 post html 时,您将需要硬编码一些 PHP。

喜欢

$actualPostId = 1234; // you get this from the database
file_put_contents ($filename, "<?php \$postID= $actualPostId;?> $rest_of_html");

You should follow Ian's advise and refactor your code to use a table. Otherwise you will need to hard code some PHP when you create the post html.

like

$actualPostId = 1234; // you get this from the database
file_put_contents ($filename, "<?php \$postID= $actualPostId;?> $rest_of_html");
悲歌长辞 2024-08-19 06:46:40

如果我正确地阅读了问题,我认为您只需将 entry_id 添加到评论表单作为隐藏字段,当有人发表评论时,将其包含为 post_id当您插入评论表时。我想那么你的表之间缺少链接。

if i am reading the problem right, i think you just need to add the entry_id to the comment form as a hidden field and when someone posts a comment, include it as post_id when you insert into the comment table. i think then you have the missing link between tables.

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