[英]how to unset a specific key value from a session array in php
我有一个用于预订产品的预订车,在某个时候,用户将能够从其购物车中删除特定的产品。
我需要从会话数组cos删除产品的帮助,在尝试删除产品时,我不断收到错误消息“致命错误:无法取消设置字符串偏移量”。
这是删除的代码
<?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"
确实需要删除产品代码有什么问题。 需要帮助。 谢谢!
由于某些原因, $_SESSION['inh_cart']
似乎是一个字符串,因此它试图取消不允许在字符串的索引$inh_arr_key
上的字符。
看起来您将其作为逗号分隔的列表...因此,要进行取消设置,需要先将其展开,然后再调用取消设置。
但这是一种糟糕的方法。.在混合索引方面,您留出了很多错误的余地。 您应该将其设置为数组,然后让php对其进行序列化/反序列化,这是正常会话行为的一部分。 另外,不要对密钥使用通用的有序数字索引,而应使用每个产品所独有的东西,例如SKU或数据库记录中的主键。
所以放在一起...
将商品添加到购物车:
$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; ?>
我的猜测是您的错误在这里:
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
设置/取消购物车中的商品。 这是一个非常糟糕的主意,因为当有人使用前进/后退按钮浏览您的网站时,他们(会在自己的脑海中)看到购物车发生了变化。
您可以尝试确保密钥存在。
if (array_key_exists($inh_arr_key, $_SESSION['inh_cart'])) {
unset($_SESSION['inh_cart'][$inh_arr_key]);
}
按照@prodigitalson确保$_SESSION['inh_cart']
是数组而不是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.