将ID数据传递到新页面

时间:2013-03-24 22:20:37

标签: php mysql

我是PHP mySQL的新手,所以请放心。我正在一个页面上构建主题公园列表,当您单击其中一个公园时,新页面会加载该公园的信息。我很难将主题公园ID从一个页面传递到下一页,并且无法弄清楚我做错了什么。请帮忙。

列表页面的代码:

    <?php

try
{
$pdo = new PDO('mysql:host=localhost;dbname=danville_tpf', 'username',
'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}


$output = 'Theme Park Database initialized';
include 'output.html.php';

try
{
$sql = 'SELECT park_id, name, town, state, country
FROM tpf_parks ORDER BY name ASC';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching parks: ' . $e->getMessage();
include 'error.html.php';
exit();
}

$output = 'Parks Loaded';
include 'output.html.php';

foreach ($result as $row)
{
$parklist[] = array(
'park_id' => $row['park_id'],
'name' => $row['name'],
'town' => $row['town'],
'state' => $row['state'],
'country' => $row['country']

);
}
include 'parks.html.php';

parks.html.php是这样的:

 <?php foreach ($parklist as $park): ?>


<a href="paging.php?park_id=<?php echo $park['park_id'];?>">

<h2><?php echo $park['name']; ?></h2>

<h3><?php echo $park['town'] , ', ', $park['state'] , ', ', $park['country']      ; ?></h3>

<hr>

</a>

<?php endforeach; ?>

和详细信息应加载的内容页面是:

<?php

try
{
$pdo = new PDO('mysql:host=localhost;dbname=danville_tpf', 'username',
'pasword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}


$output = 'Theme Park Database initialized';
include 'output.html.php';

try
{
$park_id = $_GET['park_id'];
$query="SELECT * FROM tpf_parks WHERE park_id = $park_id";
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching park details: ' . $e->getMessage();
include 'error.html.php';
exit();
}
?>

我认为park_id正在传递,因为内容页面的URL在结尾处显示了这个,其中的数字与park_id匹配,但我从查询中得到错误

“获取公园详细信息时出错:SQLSTATE [42000]:语法错误或访问冲突:1065查询为空”

我做错了什么?请帮忙。 丹

1 个答案:

答案 0 :(得分:1)

问题是使用变量$sql的以下行似乎不存在:

$result = $pdo->query($sql);

尝试以下方法:

$park_id = $_GET['park_id'];
$query="SELECT * FROM tpf_parks WHERE park_id = $park_id";
$result = $pdo->query($query);

注意到我已将$sql替换为$query