1

我有一个页面显示我的数据库中的文章标题。它们是链接,单击时转到我想根据 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>

但是当我点击一个链接时,url 会显示正确的 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>
4

2 回答 2

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 是数字)

于 2012-05-03T18:56:34.547 回答
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..
于 2012-05-03T18:52:20.543 回答