Remove selected items from $_POST / $_SESSION array
<p>I am posting from a form that selects products from a list, to a page with the selected products displayed. I want to have a link next to each item for removing an item from the selected list (array).</p>

<p>How do I do that? I seem to be losing the session once I click on the remove link.</p>

<pre><code>session_start();

foreach($_SESSION['id'] as $key =&gt; $value){

      $array = explode(',', $value);

      if($value[0]!=''){
        $id = $array[0];
        $query = "SELECT * FROM products WHERE id = '$id'";
        $result = mysqli_query($dbc, $query);

          while ($row = mysqli_fetch_array($result)) {

            $product_id = $row['id'];

            echo '&lt;tr valign="bottom"&gt;';
            echo '&lt;td&gt;' . stripslashes($row['category']) . '&lt;/a&gt;&lt;/td&gt;';
            echo '&lt;td&gt;' . stripslashes($row['itemDesc']) . '&lt;/a&gt;&lt;/td&gt;';
            echo '&lt;td class="right"&gt;' . stripslashes(number_format($row['points'], 2)) . '&lt;/a&gt;&lt;/td&gt;';
            echo '&lt;td&gt;&lt;a href="' . $_SERVER['PHP_SELF'] . '?action=remove&amp;key=' . $key . '&amp;s=' . $_SESSION['id'] . '"&gt;Remove&lt;/a&gt;&lt;/td&gt;';
            echo "&lt;/tr&gt;\n\n";
            $points = stripslashes($row['points']);
            @$points_total += $points;
          }
      }
      }


$postid = $_POST['id'];
$_SESSION['id'] = $_POST['id'];

$product_id = htmlspecialchars(@$_GET['id'], ENT_QUOTES, 'UTF-8');//the product id from the URL
$s = $_SESSION['id'];
$s = htmlspecialchars(@$_GET['key'], ENT_QUOTES, 'UTF-8');//the product id from the URL
$action = htmlspecialchars(@$_GET['action'], ENT_QUOTES, 'UTF-8'); //the action from the URL

switch($action) {
    case "remove":
        unset($array[$id]); //remove $product_id from the array with
        echo $action . $product_id;
break;
    }
</code></pre>

<p>Here's the HTML for the form:</p>

<pre><code> &lt;form method="post" action="products_selected.php"&gt;
&lt;?php

  $query = "SELECT * FROM products ORDER BY rangeCode, category ASC";
  $result = mysqli_query($dbc, $query);
  while ($row = mysqli_fetch_array($result)) {

      $id = $row['id'];

    echo '&lt;tr valign="bottom"&gt;';
    echo '&lt;td&gt;' . stripslashes($row['rangeCode']) . '&lt;/td&gt;';
    echo '&lt;td&gt;' . stripslashes($row['category']) . '&lt;/a&gt;&lt;/td&gt;';
    echo '&lt;td&gt;' . stripslashes($row['itemDesc']) . '&lt;/a&gt;&lt;/td&gt;';
    echo '&lt;td&gt;' . number_format($row['points'], 2) . ' points ';
    echo '&lt;input type="checkbox" name="id[]" value="' . $id . '" /&gt;&lt;/td&gt;';
    echo '&lt;/tr&gt;' . "\n\n";
  }
  mysqli_close($dbc);
?&gt;
 &lt;tr&gt;&lt;td colspan=13&gt;&lt;input type="submit" name="submit" value="Order" /&gt;&lt;/td&gt;&lt;/tr&gt;
</code></pre>
