如何在php中取消设置会话数组中的特定键值
我有一个用于预订产品的预订购物车,在某个时候,用户可以从购物车中删除特定的产品 我需要帮助从会话数组中删除产品,因为我在尝试删除产品时不断收到错误消息“致命错误:无法取消设置字符串偏移量” 这是删除的代码如何在php中取消设置会话数组中的特定键值,php,arrays,session,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']; u
<?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>';
}
}
?>
为了提供关于这个问题的更多细节,下面是将产品添加到购物车的代码,它工作正常
<?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'];
?>
真的需要知道删除产品的代码有什么问题。我需要帮助。谢谢 您可以尝试确保密钥存在
if (array_key_exists($inh_arr_key, $_SESSION['inh_cart'])) {
unset($_SESSION['inh_cart'][$inh_arr_key]);
}
根据@prodigitalson,确保
$\u SESSION['inh\u cart']
不是一个字符串。由于某种原因,$\u SESSION['inh\u cart']
此时似乎是一个字符串,因此它试图在字符串的索引$inh\u arr\u key
处取消设置字符,这是不允许的
它看起来像是一个逗号分隔的列表。。。因此,为了完成unset,您需要在调用unset之前将其分解
但这不是一个好办法。。在使索引混淆方面,您会留下很大的错误空间。您应该将其设置为一个数组,并让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; ?>
$c_id=$\u GET['c_id'];
会话名称(“inh_cart”);
会话_start();
如果(!isset($\u会话['inh\u cart'])){
//如果我们没有购物车-将其初始化为数组
$\会话['inh_cart']=array();
}
$inh_cart&=$u会话['inh_cart'];
如果(在数组中($c\u id,$inh\u cart)):?>
window.location=“user_allc_booking.php?ex_inh_cid=”;
window.location=“user\u allc\u booking.php”;
我猜你的错误在这里:
unset($_SESSION['inh_cart'][$inh_arr_key]);
似乎您有一个逗号分隔的值作为$\u SESSION['inh\u cart']
。它是一个字符串,而不是一个数组。因此,您在字符串上使用[]
语法实质上是在执行以下操作:
unset `$_SESSION['inh_cart']` at position starting at [some string value] ($inh_arr_key)
当然,该字符串没有[some string value]的偏移量,因为它的偏移量是严格的数字
您需要使用“$\u SESSION['inh\u cart']`作为数组
此外,您可能不想通过$\u GET
设置/取消设置购物车中的项目。这是一个非常糟糕的主意,因为当有人使用前进/后退按钮导航您的站点时,他们会(在他们的脑海中随机地)进行设置查看他们的购物车已更改。好吧,它是什么?数组还是字符串?那么哪行代码实际会发出错误?为什么您要在$\u会话['inh\u cart']
中将类似数组的数据存储为逗号分隔的值,而不仅仅是使$\u会话['inh\u cart']
成为一个更易于使用的数组?
unset($_SESSION['inh_cart'][$inh_arr_key]);
unset `$_SESSION['inh_cart']` at position starting at [some string value] ($inh_arr_key)