我对 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 在 paging.php?park_id=2 末尾显示了这一点,其编号与 park_id 匹配,但我从查询中得到一个错误,上面写着
“获取公园详细信息时出错:SQLSTATE [42000]:语法错误或访问冲突:1065 查询为空”
我做错了什么?请帮忙。担