gpt4 book ai didi

php - 将 ID 数据传递到新页面

转载 作者:行者123 更新时间:2023-11-29 00:33:09 24 4
gpt4 key购买 nike

我是 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 查询为空”

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

最佳答案

问题是以下行使用了似乎不存在的变量 $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

关于php - 将 ID 数据传递到新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15604744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com