简体   繁体   English

从会话ID为0的购物车中删除商品

[英]Remove item from cart with session id 0

I have a session id for my shopping cart. 我的购物车有一个会话ID。 This ID gets assigned to it when you place it in the shopping cart. 当您将其放入购物车时,将为其分配此ID。 For some reason I can't seem to be able to remove the first item that gets placed in the shopping cart. 由于某些原因,我似乎无法删除放入购物车中的第一个商品。 It works fine with other items that get the id 1,2 etc.. 它与ID为1,2等的其他项配合正常。

Here's the code: 这是代码:

On the index, the button to add something to the shopping cart is this: 在索引上,用于向购物车添加内容的按钮是:

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

Then the code that adds the item to the session['cart']: 然后将项目添加到会话中的代码['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');
}
?>

The shopping cart itself: 购物车本身:

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

This is the delete button: 这是删除按钮:

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

And here is the delcart.php code: 这是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');

?>

I can't seem to figure out why it can't delete the first one. 我似乎无法弄清楚为什么它不能删除第一个。 I hope you can help me out, thanks. 希望您能帮助我,谢谢。

Here's a gif of the issue: https://gfycat.com/gifs/detail/ChiefWeepyGroundbeetle 这是问题的GIF: https : //gfycat.com/gifs/detail/ChiefWeepyGroundbeetle

!empty(0) returns false , so you didn't go into the if. !empty(0)返回false ,因此您无需进入if。 You can remove it, or replace it with is_numeric($_GET['id']) . 您可以删除它,或将其替换为is_numeric($ _ GET ['id']) You also need to use && instead of & 您还需要使用&&代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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