回复注释会返回所有注册用户所需的注释

时间:2012-04-11 14:26:06

标签: php mysql

我的评论系统有问题。添加注释有效,MySql表显示已提交的正确注释。但是,当回显表的内容时,将为每个注册用户打印注释。也就是说,而不只是说... 你好詹姆斯在11/04/2012 它会说你好詹姆斯在11/04/2012你好汤姆在11/04/2012你好你好艾玛于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";
                }
            }
?>

1 个答案:

答案 0 :(得分:1)

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

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

连接只能一起连接到表,但它不会“过滤掉”结果,因为你仍然需要WHERE子句