我有一个页面显示数据库中文章的标题。它们是链接,点击后转到一个单独的页面,我想根据ID显示标题和内容。链接如下所示:
echo '<h2><a href="../../pages/content_page/contents_page.php?id='.$review_id.'">'.$title.'</a></h2>';
,显示它们的页面包含以下代码:
<?php
while ($row = mysql_fetch_assoc($query)){
$review_id = $row['review_id'];
$title = $row['title'];
$review = $row['review'];
} ?>
<h1><?php echo $title; ?></h1><br /><br />
<p><?php echo $review; ?></p>
但是当我点击链接时,网址会显示正确的ID号,但是我点击的每个链接显示的标题和内容都是相同的。如何让它们针对每个单独的ID进行更改?提前谢谢!
编辑:
完整代码:
<?php
include '../../mysql_server/connect_to_mysql.php';
$query = mysql_query("SELECT `review_id`, `title`, `review` FROM `reviews`");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Posts!</title>
<body>
<?php
while ($row = mysql_fetch_assoc($query)){
$review_id = $row['review_id'];
$title = $row['title'];
$review = $row['review'];
} ?>
<h1><?php echo $title; ?></h1><br /><br />
<p><?php echo $review; ?></p>
</body>
</html>
答案 0 :(得分:1)
您正在提取所有行。您需要WHERE
子句。
$review_id = intval($_GET['review_id']);
$resource = mysql_query("SELECT title, review FROM reviews
WHERE review_id = $review_id");
if (!$resource) {
trigger_error(mysql_error());
}
# etc..
答案 1 :(得分:1)
尝试这样的事情......
<?php
$id = $_GET['id'];
$query = mysql_query("SELECT `title`, `review` FROM `yourtable` WHERE `review_id` = '$id'");
$row = mysql_fetch_assoc($query);
//$review_id = $row['review_id']; **You will not need this because you already have it in the URL
$title = $row['title'];
$review = $row['review'];
?>
<h1><?=$title?></h1><br /><br />
<p><?=$review?></p>
编辑:更改:
$query = mysql_query("SELECT `review_id`, `title`, `review` FROM `reviews`");
要:
$query = mysql_query("SELECT `title`, `review` FROM `reviews` WHERE `review_id` = '".$_GET['id']."'");
另外......您需要对$_GET['id']
进行一些过滤,例如:
preg_match('/^[0-9]{1,9}$/', $_GET['id'])
(假设您的ID是数字)