如何从php中的会话数组中取消设置特定的键值

时间:2012-12-14 17:39:08

标签: php arrays session

我有预订产品的预订购物车,在某些时候,用户可以从购物车中删除特定产品。

我需要帮助从会话数组中删除产品我在尝试删除产品时不断收到错误消息“致命错误:无法取消设置字符串偏移”。

以下是删除代码

    <?php

if(isset($_GET['inh_arr_key']) && isset($_GET['c_id'])) {

$inh_arr_key = $_GET['inh_arr_key'];
$del_c_id = $_GET['c_id'];

unset($_SESSION['inh_cart'][$inh_arr_key]);


$inh_cart = $_SESSION['inh_cart'];

// Parse the cart session variable
$inh_arr = explode(',',$inh_cart);  

if (array_key_exists($inh_arr_key, $inh_arr)) {
    echo '<p class="err_msg">Unable to delete selected course from your In-house course booking cart.</p>';
}
else{

$del_inh_query_course_info = @mysql_query("select * from inhouse_prod where id='".$del_c_id."'");
$del_inh_course_det = @mysql_fetch_assoc($del_inh_query_course_info);   
$del_inh_course_title = $del_inh_course_det['title'];

    echo '<p class="ok_msg"><strong>'.$ex_inh_course_title.'</strong> has been deleted from your In-house course booking cart.</p>';    
}

}



?>

我已经获取并附加了每个产品数组键和值。所以这是由$ _GET检索的,并且它们在这些变量中

$inh_arr_key = $_GET['inh_arr_key'];
$del_c_id = $_GET['c_id'];

只是提供有关该问题的更多详细信息,以下是将产品添加到购物车的代码,并且工作正常

<?php



$c_id = $_GET['c_id'];

session_name("inh_cart");

session_start();

$inh_cart = $_SESSION['inh_cart'];
if ($inh_cart) {

$get_inh_arr = explode(',',$inh_cart);

if(in_array($c_id, $get_inh_arr)){ 
$inh_cart = $inh_cart;

?>
    <script language="javascript">
    window.location = "user_allc_booking.php?ex_inh_cid=<?php echo $c_id; ?>";
    </script>
<?php

}
else {
$inh_cart .= ','.$c_id;
}


} else {
$inh_cart = $c_id;
}
$_SESSION['inh_cart'] = $inh_cart;


?>
    <script language="javascript">
    window.location = "user_allc_booking.php";
    </script>
<?php



$inh_query_course_info = @mysql_query("select * from inhouse_courses where id='".$c_id."'");
$inh_course_det = array();
$inh_course_det = @mysql_fetch_assoc($inh_query_course_info);   
$inh_course_title = $inh_course_det['title'];




?>

如果例如购物车包含3个产品,如果我执行var_dump($ _ SESSION ['inh_cart']);

输出将是:

string(8) "20,48,24"

真的需要删除产品的代码有什么问题。需要帮助。谢谢!

3 个答案:

答案 0 :(得分:2)

由于某种原因,$_SESSION['inh_cart']此时似乎是一个字符串,所以它试图取消字符串$inh_arr_key处的字符串,这是不允许的。

看起来你把它作为一个以逗号分隔的列表...所以为了解决你的问题,你需要在调用未设置之前将其爆炸。

但这是一个不好的方法。你在索引混乱方面留下了很多错误的空间。你应该让它成为一个数组,让php序列化/反序列化它作为正常会话行为的一部分。此外,不使用通用的有序数字索引作为键,而是使用每个产品的独特内容,如SKU或DB记录中的主键。

所以把它放在一起......

将东西添加到购物车:

$c_id = $_GET['c_id'];

session_name("inh_cart");

session_start();

if(!isset($_SESSION['inh_cart']) {
  // if we dont have a cart - initialize it as an array
  $_SESSION['inh_cart'] = array();
}

$inh_cart &= $_SESSION['inh_cart'];

if(in_array($c_id, $inh_cart)): ?> 
    <script language="javascript">
       window.location = "user_allc_booking.php?ex_inh_cid=<?php echo $c_id; ?>";
    </script>
<?php else: 

   // just append the item to the array 
   $inh_cart[] = .$c_id;

endif; ?>

<script language="javascript">
    window.location = "user_allc_booking.php";
</script>

<?php

// not sure what youre trying to do here but ok...
$inh_query_course_info = @mysql_query("select * from inhouse_courses where id='".$c_id."'");
$inh_course_det = array();
$inh_course_det = @mysql_fetch_assoc($inh_query_course_info);   
$inh_course_title = $inh_course_det['title'];

?>

然后从购物车中删除:

<?php
// all processing at the top - easier to read -
// use the $error variable to tell what message to display
$error = false;

if(!isset($_GET['c_id'])) {
  $error = true;
} else {

  $del_c_id = $_GET['c_id'];
  $del_key = array_search($del_c_id, $_SESSION['inh_cart']);


  if($del_key) {
      unset($_SESSION['inh_cart'][$delkey]);

      // get the course info
      $del_inh_query_course_info = @mysql_query("select * from inhouse_prod where id='".$del_c_id."'");
      $del_inh_course_det = @mysql_fetch_assoc($del_inh_query_course_info);   
      $del_inh_course_title = $del_inh_course_det['title'];
  } else {
     $error = true;
  }
}
?>

<?php if($error): ?>
    <p class="err_msg">Unable to delete selected course from your In-house course booking cart.</p>
<?php else: ?>
    <p class="ok_msg"><strong> <?php echo $ex_inh_course_title ?></strong> has been deleted from your In-house course booking cart.</p>
<?php endif; ?>

答案 1 :(得分:1)

我的猜测是你的错误在这里:

unset($_SESSION['inh_cart'][$inh_arr_key]);

您似乎将逗号分隔值设为$_SESSION['inh_cart']。它是一个字符串,而不是一个数组。因此,在字符串上使用[]语法实质上是这样的:

unset `$_SESSION['inh_cart']` at position starting at [some string value] ($inh_arr_key)

当然,该字符串没有[某些字符串值]的偏移量,因为它的偏移量是严格的数字。

你需要使用'$ _SESSION ['inh_cart']`作为数组。

此外,您可能不希望通过$_GET在购物车中设置/取消设置项目。这是一个非常糟糕的主意,因为当有人使用前进/后退按钮浏览您的网站时,他们会(随意在他们的脑海中)看到他们的购物车发生了变化。

答案 2 :(得分:0)

您可以尝试确保密钥存在。

if (array_key_exists($inh_arr_key, $_SESSION['inh_cart'])) {
    unset($_SESSION['inh_cart'][$inh_arr_key]);
}

根据@prodigitalson确保$_SESSION['inh_cart']数组不是字符串。