我正在尝试创建一个论坛。在这个准备好的声明中,我试图获得forum_topic和forum_posts。此外,在此代码中,我试图查看针对单个主题的帖子,就像您在此处查看我的主题并看到帖子(评论)一样。
我获得了$tid
和$cid
的正确值。我认为问题必须与我的准备部分有关,但我不太确定。我得到了其他陈述......
echo "<p>This topic does not exist.</p>";
但这是一个实际主题,应该有一个帖子。
有人看到我做错了吗?
我的完整代码:
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
$con = new mysqli("localhost", "", "", "");
if (mysqli_connect_errno()) {
throw new Exception("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
//Prepare
if ($stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {
$stmt->bind_param("ii", $cid, $tid);
$stmt->execute();
$stmt->fetch();
if (!$stmt) {
throw new Exception($con->error);
}
}
$stmt->store_result();
$numrows = $stmt->num_rows;
if($numrows == 1){
echo "<table width='100%'>";
if ( $_SESSION['user'] ) {
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onclick=\"window.location =
'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
} else {
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
}
foreach($stmt as $row) {
//Prepared SELECT stmt to get forum posts
if($stmt2 = $con->prepare("SELECT * FROM forum_posts WHERE `category_id`=? AND `topic_id`=?")) {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
}
while (mysqli_stmt_fetch($stmt)) {
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
}
} else {
echo "<p>This topic does not exist.</p>";
}
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage();
}
我认为显示我想要提取的数据库表可能会有所帮助。
forum_topics
CREATE TABLE `forum_topics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`topic_title` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`topic_creator` int(11) NOT NULL,
`topic_last_user` int(11) DEFAULT NULL,
`topic_date` datetime NOT NULL,
`topic_reply_date` datetime NOT NULL,
`topic_views` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_c
forum_posts
CREATE TABLE `forum_posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`topic_id` int(11) NOT NULL,
`post_creator` int(11) NOT NULL,
`post_content` text COLLATE utf8_unicode_ci NOT NULL,
`post_date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_c