PHP pdo fetch 从错误的表中生成错误的项目


php pdo fetch generates wrong items from wrong table

对于我的新闻和评论系统,我只是添加了添加评论的功能。现在的问题是,每次我添加评论时,新闻帖子也会以某种方式重复。所以 3 条评论 = 3 条新闻。

我的数据库包括:tbl_news:news_idtbl_comments: comment_id, 新闻, ...

这是我的代码

索引.php

<?php
require 'design/header.php';

// if(isset($_SESSION['user'])) {
//  echo "Welcome " . $_SESSION['user'];
//  echo "<br><a href='controllers/logout.php'>Logout</a>";
// }
if(!isset($_SESSION['user'])) {
    echo "<form action='controllers/auth.php' method='POST' class='navbar-form'>";
    echo "<div class='form-group' style='margin-right:1px;'><input size='16' type='email' name='email' class='form-control'></div>";
    echo "<div class='form-group' style='margin-right:1px;'><input size='12' type='password' name='password' class='form-control'></div>";
    echo "<input type='submit' name='submitLogin' class='btn btn-success' value='Login'>";
    echo "</form>";
    if(!isset($_SESSION['registered'])) {
        echo "<h2>Don't have an account yet?</h2>";
        echo "<a href='register.php'>Register now!</a>";
    }else {
        echo "<p class='text-success'>" . $_SESSION['registered'] . "</p>";
        session_unset();
    }
}
if(isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
}
?>
<h2>Latest news</h2>
<div class="news" style="width:60%;">
<?php
$newsQuery = $db->prepare("SELECT * FROM tbl_news LEFT JOIN tbl_comments ON tbl_news.news_id = tbl_comments.newsid ORDER BY tbl_news.news_id DESC LIMIT 5");
$newsQuery -> execute();


while($newsFetch = $newsQuery->fetch(PDO::FETCH_OBJ)) {
    $shortText = mb_substr($newsFetch->text, 0, 150);
    $text = $newsFetch->text;
echo "<div class='news-post'><h3 class='post-title'>" . $newsFetch->title . "</h3>
        <p style='float:left'>By: <span class='post-author'>" . $newsFetch->author . "</span></p>
        <p style='float:right'>Date: <span class='post-date' style='font-style:italic;'>" . $newsFetch->date . "</span></p>
        <br><p style='font-weight:normal'>";
        if(strlen($text) > 150) {
            echo $shortText . "... <a href='#'>Read more</a>";
        } else {
            echo $text;
        }
        echo "</p>";
        if(isset($_SESSION['user']) && ($newsFetch->comments == '1')) {
            $news_id = $newsFetch->news_id;
            $countComments = $db->prepare("SELECT * FROM tbl_comments WHERE newsid = '$news_id'");
            $countComments->execute();
            $number_of_rows = $countComments->rowCount();
            echo "Comments(" . $number_of_rows . ")";
            echo "<div id='commentClick'>Click <a href='#' id='openForm'>here</a> to post a comment</div>";
            echo "<form action='controllers/postComment.php' method='POST' class='navbar-form' id='commentForm'><input style='margin-right:5px' type='text' size='80%' name='commentText' placeholder='Type your comment here'><input type='hidden' value='" . $user_id . "' name='userId'><input type='hidden' name='newsId' value='" . $news_id . "'><input type='submit' name='submitComment' class='btn btn-primary btn-xs' value='Send'></form></div>";
        }elseif(!isset($_SESSION['user']) && ($newsFetch->comments == '1')) {
            echo "Click here to view comments. If you want to post comments please login first</div>";
        }else{
            echo "Comments are disabled for this news item</div>";
        }
}

?>
</div>
<?php
if(isset($_SESSION['user'])) {
    echo "<a href='controllers/logout.php'>Logout</a>";
}
require 'design/footer.php';
?>
<script>
    $(function() {
        $('#openForm').click(function() {
            $('#commentForm').css('display', 'inline');
            $('#commentClick').css('display', 'none');
        });
    });
</script>

和帖子评论.php文件:

require '../config/connect.php';
if(isset($_POST['submitComment'])) {
    $commentText = $_POST['commentText'];
    $userId = $_POST['userId'];
    $newsId = $_POST['newsId'];
    $query = $db->prepare("INSERT INTO tbl_comments (newsid, user_id, comment) VALUES (:newsId, :userId, :commentText)");
    $query->bindParam("commentText", $commentText, PDO::PARAM_STR);
    $query->bindParam("userId", $userId, PDO::PARAM_STR);
    $query->bindParam("newsId", $newsId, PDO::PARAM_STR);
    if($query->execute()) {
        echo "Comment succesvol gepost";
    }
}

我找不到的另一件事是为什么只有最新的(顶部)帖子才能与jquery代码一起使用以在单击时显示表单

在查询中,联接两个表。当您这样做时,您将获得每个相应评论返回的news_item。要解决此问题,请查询新闻项,获取news_id,然后查询评论。我在手机上,所以我不能给出更好的例子,当我手边有电脑时会更新。