简体   繁体   中英

how to unset a specific key value from a session array in php

I have a booking cart for booking for products and at some point, user would be able to delete specific products from their cart.

I need help deleting product from the session array cos I keep getting an error message "Fatal error: Cannot unset string offsets" while trying to delete a product.

Here is the code for deleting

    <?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>';    
}

}



?>

I've fetched and appended each products array key and value to them. So that's being retrieved by $_GET and they are in these variables

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

Just to provide more detail about the question, here is the code for adding products to the cart and it's working fine

<?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'];




?>

And if for example the cart contain 3 product, if I do a var_dump($_SESSION['inh_cart']);

The output will be:

string(8) "20,48,24"

Really need to what's wrong with the codes for deleting products. Need help with this. Thanks!

It would seem that $_SESSION['inh_cart'] is a string at this point for some reason so its trying to unset the char at index $inh_arr_key of the string which isnt allowed.

It looks like you have it as a comma separated list... so in order to do your unset you need to explode it BEFORE calling unset.

But thats a bad way to do it.. you leave alot of room for error in terms of getting the indexes mixed up. You should just make it an array and let php serialize/unserialize it as part of the normal session behavior. Additionally instead of using generic ordered numeric indexes for the keys, use something unique to each product like the SKU or the primary key from the DB record.

So putting that together...

Adding stuff to the cart:

$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'];

?>

And then deleting from the cart:

<?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; ?>

My guess is that your error is here:

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

It seems that you have a comma-separated value as $_SESSION['inh_cart'] . It is a string, not an array. Thus your use of [] syntax on a string is in essence doing something like:

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

Of course that string does not have an offset of [some string value] as its offsets are strictly numerical.

You need to use '$_SESSION['inh_cart']` as an array.

Also, you probably don't want to set/unset items in a cart via $_GET . This is a really bad idea, as as someone navigates your site using forward/back buttons they will (randomly in their mind) see their cart changed.

You can try to make sure key exists.

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

As per @prodigitalson Make sure $_SESSION['inh_cart'] is array not a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM