在基于PHP会话的购物车中更新数量

时间:2015-07-11 13:04:31

标签: php cart updating shopping

在论坛上多次询问过这个问题,但没有一个问题得到明确解答。我是PHP编程的新手,并且正在尝试使用PHP会话构建购物车。当用户点击我的购物车中的删除链接时,数量会减少,但当数量小于1时,我的程序会显示以下错误:

  

注意:未定义的偏移量:第25行的C:\ xampp \ htdocs \ mycart \ functions.php中的0

     

注意:尝试在第25行的C:\ xampp \ htdocs \ mycart \ functions.php中获取非对象的属性

     

注意:使用未定义的常量item_delete - 在第47行的C:\ xampp \ htdocs \ mycart \ cart.php中假定为'item_delete'

以下是我的cart.php文件的源代码

<?php

require 'init.php';
require 'item.php';


//adding to cart

if (isset($_GET['pid']) && empty($_GET['pid']) === false) {

    $query = 'SELECT * FROM products where id=' . $_GET['pid'];
    $result = $con->query($query);
    $product = mysqli_fetch_object($result);

    $item = new item();

    $item->id = $product->id;
    $item->name = $product->name;
    $item->price = $product->price;
    $item->quantity = 1;

    $index = in_cart($item->id);
    $cart = unserialize(serialize($_SESSION['cart']));

    if ($index === false) {
        $_SESSION['cart'][] = $item;
    } else {
        $cart[$index]->quantity++;
        $_SESSION['cart'] = $cart;
    }

        header("Location:displaycart.php");

 }else if ( isset($_GET['view']) ){
    disp_cart();

    }


if(isset($_GET['delete']) && !empty($_GET['delete'])){

        $cart=unserialize(serialize($_SESSION['cart']));
        $item_delete=in_cart($_GET['delete']);

        if(empty($cart)){
            echo "Cart Already Empty <br></Emptybr><a href='index.php'>Continue Shopping</a>";
        }else if(!item_delete){
            echo "Item Not Found In Cart <br><a href='index.php'>Continue Shopping</a>";
        }else if($cart[$item_delete]->quantity==1){

            unset($cart[$item_delete]);
            header("Location:index.php");
            //echo"Item Deleted <br><a href='index.php'>Continue Shopping</a>";
        }else{
            $cart[$item_delete]->quantity--;
        }
$_SESSION['cart']=$cart;
    }


?>

0 个答案:

没有答案
相关问题