TomG103 TomG103 - 16 days ago 5
MySQL Question

Passing data from one page to another using PHP Sessions

I'm using sessions in PHP. At the top of both pages, I do a session_start(); before anything else.

Later on, information is pulled from a database that is used to populate the page with information. Right now I'm only using 20 different items. The first page iterates through the database and gives me output that I expect. Once you select an image, it goes to another page that should have more information on that object. The problem is, the new page always shows the last object in the database. I'll post relevant code and hope that someone can point out my failure.

<?php

//initial page with list of objects

session_start();

$_SESSION['listingID'] = $listingID;


?>

<!DOCTYPE HTML>
<html>
<head>
<title>Some Title</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="main.css" />

</head>
<body>

<!-- Wrapper -->
<div id="wrapper">

<!-- Header -->
<header id="header">
<h1><a href="#" target="__blank">Some H1 that works fine</h1>
<nav>
<ul>
<li><a href="#" target="__blank">Some link</a></li>
</ul>
</nav>
</header>
</div>



</body>
</html>

<?php

try {
$dbh = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
//echo "Connection is successful!<br/>";
$sql = "SELECT * FROM $tablename";
$users = $dbh->query($sql);



foreach ($users as $row) {
extract($row);
$_SESSION['listingID'] = $listingID;
echo "THIS IS listingID ". $listingID;
echo '<div id="main" style="margin-bottom: 2em;">';
echo '<article class="thumb">';
echo '<a href="lake_full.php?'.$listingID.'" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';
echo "<h2>$address</br>$city, $state $zip</br>$$asking_price</h2>";
echo '</article>';
echo '</div>';



} // end foreach

$dbh = null;
}
catch (PDOexception $e) {
echo "Error is: " . $e-> etmessage();
}


?>


The following code is what I'm using on the new page that should bring over the 'id' or something associated with the image selected on the first page.

<?php

//lake_full.php
session_start();

$listingID = $_SESSION['listingID'];
echo "ID IS ".$listingID;

?>


<!DOCTYPE HTML>
<html>
<head>
<title>Some Title</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />

<link rel="stylesheet" href="main.css" />

</head>
<body>

<!-- Wrapper -->
<div id="wrapper">

<!-- Header -->
<header id="header">
<h1><a href="#">Some H1</h1>
<nav>
<ul>
<li><a href="#" target="__blank" class="icon fa-info-circle">Some Link</a></li>
</ul>
</nav>
</header>



</body>
</html>

<?php

try {
$dbh = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
//echo "Connection is successful!<br/>";
$sql = "SELECT listingID FROM $tablename";
$users = $dbh->query($sql);

echo "THIS ID IS ". $mls;

echo '<div id="main" style="margin-top: 2em;">';
echo '<article class="thumb">';
echo '<a href="#" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';
echo "<h2>$address</br>$city, $state $zip</br>$$asking_price</h2>";
echo '</article>';
echo '</div>';

$dbh = null;
}
catch (PDOexception $e) {
echo "Error is: " . $e-> etmessage();
}



?>

Answer

You are currently overwriting $_SESSION['listingID'] on each iteration on your loop, which means that it will always only contain the last item.

Instead of using sessions, you should use query strings.

Change the following row in your first file:

echo '<a href="lake_full.php?'.$listingID.'" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';

to

echo '<a href="lake_full.php?listingID='.$listingID.'" class="image"><img style="width:100%;height:100%;" src="' . $image . '" /></a>';

Now on lake_full.php, instead of using sessions, you can now get the ID using the $_GET-super global:

if (!isset($_GET['listingID'])) {
    // If we didn't get an ID, we can't continue, so stop the script
    die('Invalid listing ID');
}

$listingID = $_GET['listingID'];

// ... the rest of the code...