显示评论

发布于 2024-11-13 06:20:56 字数 1005 浏览 0 评论 0原文

嘿伙计们,抱歉,如果这是一个业余问题,但我在这方面遇到了一些麻烦。

如何显示针对特定页面的评论? (page.php?id=48)

因为现在,每次我发表评论时,它都会显示在所有页面上,而不是我希望它发布在

继承人代码上的页面上:

$userfinal=$_SESSION['username'];
$rs = mysql_query("SELECT id FROM searchengine") or die(mysql_error());
$rec = mysql_fetch_assoc($rs);
$id = $rec['id'];

// get the messages from the table.
$get_messages = mysql_query("SELECT messages_id FROM messages WHERE to_user='$id' ORDER BY messages_id DESC") or die(mysql_error());

$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$id' ORDER BY messages_id DESC") or die(mysql_error());

$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content
echo '<ul>';

for($count = 1; $count <= $num_messages; $count++){
    $row = mysql_fetch_array($get_messages2); 
    // if the message is not read, show "(new)"
    // after the title, else, just show the title.
    if($row['message_read'] == 0)

任何帮助将不胜感激,谢谢

Hey guys sorry if this is an amateur question but I'm having a little trouble with this.

How do I display comments towards a specific page? (page.php?id=48)

Because right now, every time i post a comment, it displays on all pages instead of the one i wanted it to post on

Heres the code:

$userfinal=$_SESSION['username'];
$rs = mysql_query("SELECT id FROM searchengine") or die(mysql_error());
$rec = mysql_fetch_assoc($rs);
$id = $rec['id'];

// get the messages from the table.
$get_messages = mysql_query("SELECT messages_id FROM messages WHERE to_user='$id' ORDER BY messages_id DESC") or die(mysql_error());

$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$id' ORDER BY messages_id DESC") or die(mysql_error());

$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content
echo '<ul>';

for($count = 1; $count <= $num_messages; $count++){
    $row = mysql_fetch_array($get_messages2); 
    // if the message is not read, show "(new)"
    // after the title, else, just show the title.
    if($row['message_read'] == 0)

Any help would be appreciated, thanks

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

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

发布评论

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

评论(1

皇甫轩 2024-11-20 06:20:56

看看我的示例代码。

考虑一个具有基本结构的表comments

CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `comment` text NOT NULL,
  `article_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);

comment 列将保存您的评论文本

article_id 保存其所属文章的外键。

现在假设您想从特定的 articleid article.php?id=48 检索评论,

您应该这样做。

$articleId = mysql_real_escape_string($_GET['id']);
$query = 'SELECT id,comment FROM comments WHERE article_id ='.$articleId;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
    echo nl2br($row['comments']);
}

尽管我的代码与您的问题根本无关,但它应该为您提供如何实现逻辑的基本概念。

编辑:

你不应该使用代码进行生产,代码只是为了解释你实现逻辑,记住这段代码很容易受到SQL注入,如果你想要临时修复,你可以使用 mysql_real_escape_string() 函数来避免它。检查我更新的代码。

提示:您应该尝试对所有数据库查询使用 PDO,这里是帮助您入门的教程 http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

take a look at my sample code.

Consider a table comments with the basic structure.

CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `comment` text NOT NULL,
  `article_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);

comment column will hold the text of your comment

article_id holds the foreign key of the article it belongs to.

now lets say you want to retrieve the comment from a particular articleid article.php?id=48

here is how you should be doing it.

$articleId = mysql_real_escape_string($_GET['id']);
$query = 'SELECT id,comment FROM comments WHERE article_id ='.$articleId;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
    echo nl2br($row['comments']);
}

although my codes does not relate to your question at all, but it should give you the basic idea on how to implement the logic.

EDIT :

you should not use the code for production, the code is only meant to explain you to implement the logic, remember this code is vulnerable to SQL injections, if you want a temporary fix you could use mysql_real_escape_string() function to avoid it. check my updated code.

TIP : you should try and use PDO for all your database queries here is the tutorial to get you started http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

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