php - 如何根据 id PHP 从会话数组中删除变量
问题描述
添加产品后,我试图从我的购物车中删除产品。
你能帮忙吗
这是我的代码
添加到购物车
html:
<input type="hidden" name="pid" value="<?php echo $row['product_id']?>
<input type="double" name="weight" placeholder="Weight?" required>
PHP:
session_start();
$pid = $_POST['pid'];
$weight = $_POST['weight'];
$p = "pid";
$p .= (string) $pid;
$w = "weight";
$w .= (string) $pid;
$_SESSION[$p]=$pid;
$_SESSION[$w] = $weight;
if(isset($_SESSION)){
header('location:../shopping-cart.php');
}
执行后添加产品。
显示在购物车中
foreach($_SESSION as $key => $values){
if(strpos($key, "pid") !== false){
$sql="select * from products where product_id = $_SESSION[$key]";
$result=$connect->query($sql);
$r = mysqli_fetch_assoc($result);
$w = "weight";
$w .= (string) $r['product_id'];
$weight = $_SESSION[$w];
HTML PHP 代码
<?php echo $r["product_name"];?>
<?php echo 'LBP '.number_format($r['product_price'],0)?>
<input type="hidden" id="unitprice" onchange="f();" value="<?php echo $r['product_price']?>">
<input type="text" onchange="f();" id="quantity" value="<?php echo $weight ?>">
<input type="submit" value="Delete">
<input type="hidden" name="pid" value="<?php echo $r['product_id'] ?>">
<input type="hidden" name="pweight" value="<?php echo $weight ?>">
从购物车中删除不起作用任何帮助?
session_start();
$pidremove = $_POST['pid'];
$weightremove = $_POST['pweight'];
foreach($_SESSION as $key => $values){
if($_SESSION[$w]==$pidremove)
{
unset($_SESSION[$w]);
unset($_SESSION[$p]);
echo 'done';
}
}
解决方案
HTML:
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<input type="submit" name="remove" value="Remove">
PHP:
unset($_SESSION['pid' . $_POST['pid']]
unset($_SESSION['weight' . $_POST['pid']]
推荐阅读
- python - 如何修复“errLevel 17 IBM SPSS Statistics 后端未准备好”问题?
- excel - 为什么在 rng.Clear 之后没有发生格式化?
- javascript - 如何使用自定义组件解析主题标签和提及字符串
- sql-server - 我想根据一个字段“喜欢”另一个字段的内容加入 2 个表
- angular - 如何在所有 ngFor 元素中显示动画,因为它只发生在第一个元素中
- docker - VSCode Remote Container——如何在VSCode的容器实例中打开默认文件夹?
- powerbi - Power BI - 即使在过滤时也能在矩阵中保持可见的永久行
- reactjs - 如何使高图点重定向到 React 中具有值的另一个组件?
- google-chrome-extension - 如何在创建新选项卡后仅收听 tabs.onUpdated 并阻止 tabs.onActivated?
- python - PyAutoGui 找到按钮但仅在插入我的显示器时