【问题标题】:Remove item from cart with session id 0从购物车中删除会话 ID 为 0 的商品
【发布时间】:2017-11-06 14:31:11
【问题描述】:

我的购物车有一个会话 ID。当您将其放入购物车时,此 ID 会分配给它。由于某种原因,我似乎无法删除放入购物车的第一个项目。它适用于其他获得 id 1,2 等的项目。

代码如下:

在索引上,添加东西到购物车的按钮是这样的:

 <a href="addtocart.php?id=<?php echo $value['id']; ?>" class="btn btn-info" type="submit"> <img width=21 src=Images/winkelwagen.png></a>

然后是添加商品到会话的代码['cart']:

<?php
session_start();

if(isset($_GET['id']) & !empty($_GET['id'])){
    if(isset($_SESSION['cart']) & !empty($_SESSION['cart'])){

        $items = $_SESSION['cart'];
        $cartitems = explode(",", $items);
        if(in_array($_GET['id'], $cartitems)){
            header('location: homepagina.php?status=incart');
        }else{
            $items .= "," . $_GET['id'];
            $_SESSION['cart'] = $items;
            header('location: homepagina.php?status=success');

        }

    }else{
        $items = $_GET['id'];
        $_SESSION['cart'] = $items;
        header('location: homepagina.php?status=success');
    }

}else{
    header('location: homepagina.php?status=failed');
}
?>

购物车本身:

<?php
session_start();

include("header.php");
include_once("dbconnect.php");

if (empty($_SESSION['cart'])) {
    echo "U heeft geen producten in uw winkelwagen";

} else { 

$items = $_SESSION['cart'];
$cartitems = explode(",", $items);


?>

<div class="container">
    <div class="row">
        <div class="col-sm-12 col-md-10 col-md-offset-1">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Quantity</th>
                        <th class="text-center">Price</th>
                        <th class="text-center">Total</th>
                        <th> </th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $total = '';
                        $i=1;

                        foreach ($cartitems as $key=>$id) { 
                            $sql = "SELECT * FROM products WHERE id = $id";
                            $res=mysqli_query($conn, $sql);
                            $r = mysqli_fetch_assoc($res);

                            $sqlb = "SELECT * FROM brewery WHERE code = $r[brewery_code]";
                            $resb=mysqli_query($conn, $sqlb);
                            $rb = mysqli_fetch_assoc($resb);
                        ?>  
                    <tr>
                        <td class="col-sm-8 col-md-6">
                        <div class="media">
                            <img class="thumbnail pull-left" src="Images/<?php echo $r['name'] ?>.jpg" width="85" height="152" alt="..."  >
                            <div class="media-body">
                                <h4 class="media-heading"><?php echo $r['name']; ?></h4>
                                <h5 class="media-heading"> by <?php echo $rb['name'];; ?></a></h5>
                                <span>Status: </span><span class="text-success"><strong>In Stock</strong></span>
                            </div>
                        </div></td>
                        <td class="col-sm-1 col-md-1" style="text-align: center">
                        <input type="amount" class="form-control" id="exampleInputEmail1" value="1">
                        </td>
                        <?php 

                             $total = $total + $r['price'];
                             $i++;  
                            ?>
                        <td class="col-sm-1 col-md-1 text-center"><strong>€ <?php echo number_format($r['price'],2);?> </strong></td>
                        <td class="col-sm-1 col-md-1 text-center"><strong>$14.61</strong></td>
                        <td class="col-sm-1 col-md-1">
                        <button type="button" class="btn btn-danger">
                            <a href="delcart.php?remove=<?php echo $key; ?>">Verwijderen</a>
                        </button></td>
                    </tr>

                        <?php } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td><h5>Subtotal<br>Estimated shipping</h5><h3>Total</h3></td>
                        <td class="text-right"><h5><strong>$24.59<br>$6.94</strong></h5><h3>$31.53</h3></td>
                    </tr>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td>
                        <button type="button" class="btn btn-primary"> Continue Shopping </button></td>
                        <td>
                        <button type="button" class="btn btn-primary"> Checkout  
    </button>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    </div>

                        <?php } ?>

这是删除按钮:

<button type="button" class="btn btn-danger"> <a href="delcart.php?remove=<?php echo $key; ?>">Verwijderen</a> </button></td>

这里是 delcart.php 代码:

<?php 
session_start();
$items = $_SESSION['cart'];
$cartitems = explode(",", $items);
if(isset($_GET['remove']) & !empty($_GET['remove'])){
    $delitem = $_GET['remove'];
    unset($cartitems[$delitem]);
    $itemids = implode(",", $cartitems);
    $_SESSION['cart'] = $itemids;
}
header('location:winkelwagen.php');

?>

我似乎无法弄清楚为什么它不能删除第一个。我希望你能帮助我,谢谢。

这是问题的 GIF:https://gfycat.com/gifs/detail/ChiefWeepyGroundbeetle

【问题讨论】:

  • 我自己想通了,我将 !empty 更改为 NULL。在 delcart.php 中。

标签: php mysql session


【解决方案1】:

!empty(0) 返回 false,所以你没有进入 if。您可以删除它,或将其替换为 is_numeric($_GET['id'])。您还需要使用 && 而不是 &

if(isset($_GET['id']) && is_numeric($_GET['id'])){
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2013-10-17
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多