title: Passing data from one page to another using PHP Sessions
body: <p>I'm using sessions in PHP. At the top of both pages, I do a session_start(); before anything else.</p>
  
  <p>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.</p>
  
  <pre><code>&lt;?php
  
  //initial page with list of objects
  
  session_start();
  
  $_SESSION['listingID'] = $listingID;
  
  
  ?&gt;
  
  &lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Some Title&lt;/title&gt;
       &lt;meta charset="utf-8" /&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /&gt;
      &lt;link rel="stylesheet" href="main.css" /&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;!-- Wrapper --&gt;
  &lt;div id="wrapper"&gt;
  
      &lt;!-- Header --&gt;
      &lt;header id="header"&gt;
          &lt;h1&gt;&lt;a href="#" target="__blank"&gt;Some H1 that works fine&lt;/h1&gt;
          &lt;nav&gt;
              &lt;ul&gt;
                  &lt;li&gt;&lt;a href="#" target="__blank"&gt;Some link&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/nav&gt;
      &lt;/header&gt;
  &lt;/div&gt;
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
  
  try {
      $dbh = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
      //echo "Connection is successful!&lt;br/&gt;";
      $sql = "SELECT * FROM $tablename";
      $users = $dbh-&gt;query($sql);
  
  
  
      foreach ($users as $row) {
      extract($row);  
      $_SESSION['listingID'] = $listingID;
      echo "THIS IS listingID ". $listingID;
      echo '&lt;div id="main" style="margin-bottom: 2em;"&gt;';
      echo '&lt;article class="thumb"&gt;';
      echo '&lt;a href="lake_full.php?'.$listingID.'" class="image"&gt;&lt;img style="width:100%;height:100%;" src="' . $image . '" /&gt;&lt;/a&gt;';
      echo "&lt;h2&gt;$address&lt;/br&gt;$city, $state $zip&lt;/br&gt;$$asking_price&lt;/h2&gt;";
      echo '&lt;/article&gt;';
      echo '&lt;/div&gt;';  
  
  
  
     } // end foreach
  
  $dbh = null;
  }
  catch (PDOexception $e) {
      echo "Error is: " . $e-&gt; etmessage();
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <pre><code> &lt;?php
  
  //lake_full.php
  session_start();
  
  $listingID = $_SESSION['listingID'];
  echo "ID IS ".$listingID;
  
  ?&gt;
  
  
      &lt;!DOCTYPE HTML&gt;
      &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Some Title&lt;/title&gt;
          &lt;meta charset="utf-8" /&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /&gt;
  
          &lt;link rel="stylesheet" href="main.css" /&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
  
      &lt;!-- Wrapper --&gt;
      &lt;div id="wrapper"&gt;
  
          &lt;!-- Header --&gt;
          &lt;header id="header"&gt;
              &lt;h1&gt;&lt;a href="#"&gt;Some H1&lt;/h1&gt;
              &lt;nav&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;a href="#" target="__blank" class="icon fa-info-circle"&gt;Some Link&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/nav&gt;
          &lt;/header&gt;
  
  
  
      &lt;/body&gt;
      &lt;/html&gt;
  
  &lt;?php
  
  try {
      $dbh = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
      //echo "Connection is successful!&lt;br/&gt;";
      $sql = "SELECT listingID FROM $tablename";
      $users = $dbh-&gt;query($sql);
  
      echo "THIS ID IS ". $mls;
  
          echo '&lt;div id="main" style="margin-top: 2em;"&gt;';
          echo '&lt;article class="thumb"&gt;';
          echo '&lt;a href="#" class="image"&gt;&lt;img style="width:100%;height:100%;" src="' . $image . '" /&gt;&lt;/a&gt;';
          echo "&lt;h2&gt;$address&lt;/br&gt;$city, $state $zip&lt;/br&gt;$$asking_price&lt;/h2&gt;";
          echo '&lt;/article&gt;';
          echo '&lt;/div&gt;';
  
      $dbh = null;
  }
  catch (PDOexception $e) {
      echo "Error is: " . $e-&gt; etmessage();
  }
  
  
  
  ?&gt;
  </code></pre>