从会话ID为

时间:2017-11-06 14:31:11

标签: php mysql session

我的购物车有会话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>

然后将项目添加到会话的代码[&#39; cart&#39;]:

<?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');

?>

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

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

1 个答案:

答案 0 :(得分:0)

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

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