2013-03-11 67 views
2

我从表单中选择列表中的产品发布到显示所选产品的页面。我想在每个项目旁边都有一个链接,用于从选定列表(数组)中删除项目。

我该怎么做?一旦我点击删除链接,我似乎失去了会话。

session_start(); 

foreach($_SESSION['id'] as $key => $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 '<tr valign="bottom">'; 
      echo '<td>' . stripslashes($row['category']) . '</a></td>'; 
      echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>'; 
      echo '<td class="right">' . stripslashes(number_format($row['points'], 2)) . '</a></td>'; 
      echo '<td><a href="' . $_SERVER['PHP_SELF'] . '?action=remove&key=' . $key . '&s=' . $_SESSION['id'] . '">Remove</a></td>'; 
      echo "</tr>\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; 
    } 

这里的窗体的HTML:

<form method="post" action="products_selected.php"> 
<?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 '<tr valign="bottom">'; 
    echo '<td>' . stripslashes($row['rangeCode']) . '</td>'; 
    echo '<td>' . stripslashes($row['category']) . '</a></td>'; 
    echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>'; 
    echo '<td>' . number_format($row['points'], 2) . ' points '; 
    echo '<input type="checkbox" name="id[]" value="' . $id . '" /></td>'; 
    echo '</tr>' . "\n\n"; 
    } 
    mysqli_close($dbc); 
?> 
<tr><td colspan=13><input type="submit" name="submit" value="Order" /></td></tr> 
+0

您能向我们展示表单的HTML代码吗?谢谢 – 2013-03-11 09:51:04

+0

@Erenor我编辑的问题,包括格式HTML – Grant 2013-03-11 10:05:45

+1

@Grant你试过'unset($ array [$ product_id]);'?顺便说一句,那些'@'是非常丑陋的,尝试使用'isset()'或'array_key_exists()':) – HamZa 2013-03-11 10:23:16

回答

3

确定。围绕这个问题进行了一些聊天和合作之后,我们发现了一些问题。

  1. 有插入周围使用$ _GET和$ _POST数据,以避免不必要的修改其他变量(一个例子的代码检查的需要:当用户点击“删除”,从他的选择中删除项目,$ _SESSION数组将被$ _POST数组更新;因为这不包含任何内容,会话数组将被清空(这就是为什么会话被认为丢失的原因):
  2. 查找和删除会话中,我们必须使用从url中检索到的密钥并检查它是否存在于会话数组中,这可以在下面的代码中看到:

    if (isset($_POST['id'])) 
    { 
        $_SESSION['id'] = $_POST['id']; 
    } 
    
    if(isset($_GET['key']) && ($_GET['action'] == 'remove')) 
    { 
        if (array_key_exists($_GET['key'], $_SESSION['id'])) 
        { 
        unset($_SESSION['id'][$_GET['key']]); 
        } 
    } 
    

对代码进行了一些其他细微的更改,但主要问题是解释的主要问题。

+0

再次感谢您的意见。 – Grant 2013-03-11 16:28:19

+0

很高兴与你合作:) – 2013-03-11 16:35:10