gpt4 book ai didi

php - 回显评论会返回所有注册用户的所需评论

转载 作者:行者123 更新时间:2023-11-29 14:30:33 25 4
gpt4 key购买 nike

我的评论系统有问题。添加评论有效,MySql 表显示已提交的正确评论。然而,当回显表的内容时,将为每个注册用户打印注释。即,不是只说... Hello by James on 11/04/2012 而是说 Hello by James on 11/04/2012 Hello by Tom on 11/04/2012 Hello by艾玛于 11/04/2012

正如我所说,MySql 表仅显示一条评论,并且显示上传评论的用户的正确 user_id 编号(与 users 表中的 id 相对应)。这一定是我回应评论的方式,但我是 PHP 和 MySql 的新手,我不确定我是否正确加入了表格。

如果有任何帮助,我将不胜感激。

require_once("db_connect.php");
if($db_server) {
//If a connection to the database is made...
mysql_select_db($db_database) or die ("<p>Couldn't find database</p>");

//Print out existing comments
$result = mysql_query("SELECT * FROM comments JOIN users ON comments.profile_id = $problemID");
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j=0; $j<$rows; ++$j) {
$str_comments .= "<span class='comment'>" . mysql_result($result, $j, 'comment') . "</span>";
$str_comments .= " bt " . mysql_result($result, $j, 'username');
$str_comments .= " on " . mysql_result($result, $j, 'comm_date') . "</br><br/>";
}

//Get any submitted comments and insert into database
$comment = clean_string($_POST['comment']);
if ($comment != '') {
//If the submitted comment is not empty...
if (strlen($comment)<200) {
mysql_query("INSERT INTO comments (`user_id`, `profile_id`, `comment`)
VALUES ('{$_SESSION['user_id']}', $problemID, ('$comment'))") or die(mysql_error());
}else{
$message = "Comment not submitted. The comment must be less than 200 characters long";
}
}
?>

最佳答案

在您的查询中我没有看到 WHERE 子句。应该是这样的:

$result = mysql_query("SELECT * FROM comments JOIN users ON comments.profile_id = $problemID WHERE users.id=$userid");

联接仅将表联接在一起,但它不会“过滤掉”结果,因为您仍然需要 WHERE 子句

关于php - 回显评论会返回所有注册用户的所需评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com