themeparkfocus
themeparkfocus

Reputation: 187

Passing ID data to new page

I am very new to PHP mySQL so please go easy. I'm building a list of theme parks on one page, when you click one of the them parks a new page loads with info on that park. I am having difficultly passing on the theme park ID from one page to next and can't work out what I'm doing wrong. Please help.

The code for the list page:

    <?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';

The parks.html.php is this:

 <?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; ?>

and the content page where the details should load is:

<?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();
}
?>

I think the park_id is being passed because the URL of the content page shows this at the end paging.php?park_id=2 with the number matching the park_id but I get an error from the query that says

"Error fetching park details: SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty"

What have I done wrong? please help. Dan

Upvotes: 1

Views: 791

Answers (1)

Martin
Martin

Reputation: 16433

The problem is the following line which is using the variable $sql which doesn't appear to exist:

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

Try the following:

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

Noting that I have replaced $sql with $query.

Upvotes: 1

Related Questions