页面未输出与ID关联的正确数据

时间:2015-08-10 20:07:12

标签: php mysql prepared-statement

我正在设计一个论坛。我要提到的页面是discussion.php和forum_view_category.php

讨论是显示不同类别的页面。

forum_view_categories是显示该类别内主题的页面。

截至目前,我只测试了这个,只有两个类别。它们的id为1和2.每当我点击链接转到forum_view_category页面时,会显示正确的链接,其中包含我点击的ID的相应ID。

我的问题是类别1的帖子显示在第1类和第2类中。我在第2类中没有任何主题,所以我的其他语句应该回应。

我的讨论页面有此链接可以访问相应的forum_view_category页面..

$categories = "<a href='forum_view_category.php?cid=".$categoryid."'>" . $categoryTitle . "</a>";

然后这是forum_view_category页面。

$cid = $_GET['cid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );

if(isset( $_SESSION['user'])) {
    $logged = " | <a href='forum_create_topic.php?cid=".$cid."'>Create a new topic</a>";
}
$query = mysqli_query($con,"SELECT * FROM forum_categories WHERE id='".$cid."' LIMIT 1");
$numrows = mysqli_num_rows($query);
if($numrows == 1){
    //$query2 = mysqli_query($con,"SELECT * FROM forum_topics WHERE `category_id` ='".$cid."' ORDER BY topic_reply_date DESC")
    $query2 = mysqli_query($con,"SELECT t.*, COUNT(p.topic_id) AS tid2 
FROM forum_topics AS t JOIN forum_posts AS p on t.id = p.topic_id 
GROUP BY t.id DESC")
    or die ("Query2 failed: %s\n".($query2->error));
    $numrows2 = mysqli_num_rows($query2);
    //if ( false===$query2 ) {
        // die(' Query2 failed: ' . htmlspecialchars($query2->error));
    //}
    if($numrows2 > 0){
        $topics .= "<table width='100%' style='border-collapse: collapse;'>";
        //Change link once discussion page is made
        $topics .= "<tr><td colspan='3'><a href='discussions.php'>Return to Discussion Index</a>".$logged."<hr /></td></tr>";
        $topics .= "<tr style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Replies</td><td width='65' 
        align='center'>Views</td></tr>";
        $topics .= "<tr><td colspan='3'><hr /></td></tr>";
        while($row = mysqli_fetch_assoc($query2)){
            $tid = $row['id'];
            $title = $row['topic_title'];
            $views = $row['topic_views'];
            $replies = $row['tid2'];
            $date = $row['topic_date'];
            $date = fixDate($date);
            $creator = $row['topic_creator'];
            $topics .= "<tr><td><a href='forum_view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted 
            by: ". $creator. " " . $date."</span></td><td align='center'>".$replies."</td><td align='center'>".$views."</td></tr>";
            $topics .= "<tr><td colspan='3'><hr /></td></tr>";
        }
        $topics .="</table>";
        echo $topics;
    } else {
        echo "<a href='discussions.php'>Return to Discussions page</a><hr />";
        echo "<p>There are no topics in this category yet. ".$logged." </p>";
    }
} else {
    echo "<a href='discussions.php'>Return to Discussions page</a><hr />";
    echo "<p>You are trying to view a category that does not exist yet.</p>";
}

我将一些查询更改为JOIN,以便我可以在整个网站上获取其他信息,我不确定我是否在其中做错了什么。所以,我不确定为什么它正在抓取正确的id,而不是显示其中的实际内容。有没有人有任何想法?

我认为它必须专门针对此查询......

//$query2 = mysqli_query($con,"SELECT * FROM forum_topics WHERE `category_id` ='".$cid."' ORDER BY topic_reply_date DESC")
$query2 = mysqli_query($con,"SELECT t.*, COUNT(p.topic_id) AS tid2 

FROM forum_topics AS t JOIN forum_posts AS p on t.id = p.topic_id GROUP BY t.id DESC&#34;)

注释掉的查询是我的旧查询。如何将旧查询加入新查询?

1 个答案:

答案 0 :(得分:2)

简单地说 -

mysqli_query($con,"SELECT t.*, COUNT(p.topic_id) AS tid2 FROM forum_topics AS t JOIN forum_posts AS p on t.id = p.topic_id WHERE t.category_id = ".$cid." GROUP BY t.id DESC")
or die ("Query2 failed: %s\n".($query2->error));

在您的查询2结束时