【问题标题】:php pdo fetch generates wrong items from wrong tablephp pdo fetch 从错误的表中生成错误的项目
【发布时间】:2014-12-14 16:37:43
【问题描述】:

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

我的数据库包括: tbl_news:news_id tbl_cmets:comment_id,newsid,......

这是我的代码

index.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>

还有 postComment.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 代码一起使用以在点击时显示表单

【问题讨论】:

  • 出现问题的结果截图:puu.sh/duqdS/a11f8b1b07.png
  • 会话在标题中。但这不是问题
  • 在您的 jquery 评论/问题上 - ids 应该是唯一的。但是由于您在循环中打印&lt;div id='commentClick'&gt;,因此您将拥有n 数量的id='commentClick'。由于 javascript/jQuery 正在寻找唯一的id,它只会找到第一个。您要么需要创建唯一的ids,要么更改为class

标签: php jquery database pdo


【解决方案1】:

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

【讨论】:

  • 好的,谢谢。希望看到你更好的解释。但是你不能获取 2 次对吗?
  • 您需要单独查询。
猜你喜欢
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 2013-09-12
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 2017-02-15
相关资源
最近更新 更多