title: SELECT query for selecting logged in users rows only - PHP/MySQL
body: <p>I have a members only website in which the logged in users fill in an entry form which goes to a MySQL table called <strong>'ltd_sales_list'</strong> with the following columns:</p>
  
  <blockquote>
    <p><strong>ltd_item_id</strong> | <strong>ltd_user_id</strong> |
    <strong>ltd_invoice_no</strong> | <strong>ltd_entry_amount</strong> |
    <strong>ltd_entry_date</strong></p>
  </blockquote>
  
  <p>For each new entry they input, a new id/primary key is generated (<strong>'ltd_item_id'</strong>) for each row, while their SESSION log in id is recorded in <strong>'ltd_user_id'</strong> while the <strong>'ltd_entry_date'</strong> is a timestamp. The entry form page works fine but viewing the entry data is where I am having the issue.</p>
  
  <p>I have put together the code below called <strong>view-list.php</strong> but this calls up every user's entry list. What I am trying to do is show the <strong>logged in user's entry list only</strong>.</p>
  
  <p>I think the answer lies within the queries somewhere and have tried some WHERE statements with ltd_user_id = $_SESSION['ltd_user_id'] and similar but for no success.</p>
  
  <p>If anyone could help or could point me to some links that would be greatly appreciated!</p>
  
  <pre><code>&lt;?php 
  
  require_once ('./includes/config.inc.php');
  
  $page_title = 'Page Title';
  include ('./includes/header.html');
  
  if (!isset($_SESSION['ltd_user_id'])) {
  
     $url = 'http://' . $_SERVER['HTTP_HOST']
      . dirname($_SERVER['PHP_SELF']);
  
     if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
          $url = substr ($url, 0, -1); 
     }
  
     $url .= '/login.php'; 
  ob_end_clean(); 
  header("Location: $url"); 
  exit(); 
  }
  ?&gt;
  
  &lt;div id=""&gt;HTML Content HERE&lt;/div&gt;
  
  &lt;?php
  
  echo '&lt;h1&gt;My Entry Log&lt;/h1&gt;';
  
  require_once ('/server/database_connection.php'); // Connect to the db.
  
  $display = 10;
  
  if (isset($_GET['np'])) { 
      $num_pages = $_GET['np'];
  } else { 
  
      $query = "SELECT COUNT(*) FROM ltd_sales_list ORDER BY ltd_entry_date DESC";   
      $result = @mysql_query ($query);
      $row = mysql_fetch_array ($result, MYSQL_NUM);
      $num_records = $row[0];
  
      if ($num_records &gt; $display) { 
         $num_pages = ceil ($num_records/$display);
      } else {
         $num_pages = 1;
      }
  } 
  
  if (isset($_GET['s'])) {
      $start = $_GET['s'];
  } else {
      $start = 0;
  }
  
  $link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
  $link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
  $link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
  
  if (isset($_GET['sort'])) {
  
      switch ($_GET['sort']) {
         case 'lna':
            $order_by = 'ltd_invoice_no ASC';
            $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
            break;
         case 'lnd':
            $order_by = 'ltd_invoice_no DESC';
            $link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
            break;
         case 'fna':
            $order_by = 'ltd_entry_amount ASC';
            $link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
            break;
         case 'fnd':
            $order_by = 'ltd_entry_amount DESC';
            $link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
            break;
         case 'dra':
            $order_by = 'ltd_entry_date ASC';
            $link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
            break;
         case 'drd':
            $order_by = 'ltd_entry_date DESC';
            $link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
            break;   
         default:
            $order_by = 'ltd_entry_date DESC';
            break;
      }
  
      $sort = $_GET['sort'];
  
  } else { 
      $order_by = 'ltd_entry_date DESC';
      $sort = 'dra';
  }
  
  $query = "SELECT ltd_invoice_no, ltd_entry_amount,  
      DATE_FORMAT(ltd_entry_date, '%M %d, %Y') AS dr, ltd_user_id FROM ltd_sales_list ORDER BY
      $order_by LIMIT $start, $display";
  $result = @mysql_query ($query); 
  
  echo '&lt;table width="520" cellspacing="1" cellpadding="11"&gt;
  &lt;tr&gt;
      &lt;td align="left"&gt;&lt;b&gt;&lt;a href="' . $link1 . '"&gt;Invoice Number&lt;/a&gt;&lt;/b&gt;&lt;/td&gt;
      &lt;td align="left"&gt;&lt;b&gt;&lt;a href="' . $link2 . '"&gt;Invoice Amount&lt;/a&gt;&lt;/b&gt;&lt;/td&gt;
   &lt;td align="left"&gt;&lt;b&gt;&lt;a href="' . $link3 . '"&gt;Date Entered&lt;/a&gt;&lt;/b&gt;&lt;/td&gt;
  &lt;/tr&gt;
  ';
  
  $bg = '#eeeeee'; 
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $bg = ($bg=='#eaeced' ? '#ffffff' : '#eaeced');  
      echo '&lt;tr bgcolor="' . $bg . '"&gt;
         &lt;td align="left"&gt;' . $row['ltd_invoice_no'] . '&lt;/td&gt;
         &lt;td align="left"&gt;' . $row['ltd_entry_amount'] . '&lt;/td&gt;
         &lt;td align="left"&gt;' . $row['dr'] . '&lt;/td&gt;
      &lt;/tr&gt;
      ';
  }
  echo '&lt;/table&gt;';
  mysql_free_result ($result); 
  mysql_close(); 
  
  if ($num_pages &gt; 1) {
      echo '&lt;br /&gt;&lt;p&gt;';
      $current_page = ($start/$display) + 1;
  
      if ($current_page != 1) {
         echo '&lt;a href="view-list.php?s=' . ($start - $display) . '&amp;np=' .
           $num_pages . '&amp;sort=' . $sort .'"&gt;Previous&lt;/a&gt; ';   
      }
      for ($i = 1; $i &lt;= $num_pages; $i++) {
         if ($i != $current_page) {
            echo '&lt;a href="view-list.php?s=' . (($display * ($i - 1))) . 
              '&amp;np=' . $num_pages . '&amp;sort=' . $sort .'"&gt;' . $i . '&lt;/a&gt; ';   
         } else {
            echo $i . ' ';
         }
      }
      if ($current_page != $num_pages) {
         echo '&lt;a href="view-list.php?s=' . ($start + $display) . '&amp;np=' . 
           $num_pages . '&amp;sort=' . $sort .'"&gt;Next&lt;/a&gt; ';   
      }
  
      echo '&lt;/p&gt;';
  
  } 
  
  ?&gt;
  
  &lt;div id=""&gt;HTML Content HERE&lt;/div&gt;
  
  &lt;?php
  include ('./includes/footer.html');
  ?&gt;
  </code></pre>
  
  <p>Cheers
  Adam</p>