dqqxkq4047 2013-03-11 09:45
浏览 13
已采纳

从$ _POST / $ _SESSION数组中删除所选项

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).

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

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>

";
            $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;
    }

Here's the HTML for the form:

 <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>' . "

";
  }
  mysqli_close($dbc);
?>
 <tr><td colspan=13><input type="submit" name="submit" value="Order" /></td></tr>
  • 写回答

1条回答 默认 最新

  • douangzhao4108 2013-03-11 16:25
    关注

    Ok. After a bit of chat and co-working around this issue, we found some problems.

    1. There's the need to insert a check around the code that uses $_GET and $_POST data, to avoid unwanted modification to other variables (an example: when the user clicks "Remove" to remove an item from his choices, the $_SESSION array will be updated with the $_POST array; since this contains nothing, the session array is emptied (and this was why the session was thought to be lost):
    2. To find and delete the item from the session, we have to use the key retrieved from url and check if it's present into the session array. This can be seen in the code below.

      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']]); 
        } 
      } 
      

    Some other minor changes have been made to the code, but the main problems were the ones explained.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 logisim中设计一个位于十字路口的交通信号灯控制系统
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码