PHP电子商务购物车更新PDO

问题描述

我是PHP新手。增加数量按钮不起作用。我正在尝试不使用ajax。 谢谢

购物车文件: ... &key_cartitem_increase = ok“> +

....

pdo文件

    if (isset($_GET['key_cartitem_increase'])) {
    if($_GET['key_cartitem_increase']=="ok"){
         
        $cart_id=$_GET['tblcart_id'];

        $increaseitem=$db->prepare("UPDATE tbl_cart SET         
                        tblcart_userid=:tblcart_userid,tblcart_productid=:tblcart_productid,tblcart_quantity=:tblcart_quantity+1                    
                        WHERE tblcart_id=id");

        $kontrol=$increaseitem->execute(array(
            'id' => $_GET['tblcart_id'],'tblcart_userid' => $_GET['tblcart_userid'],'tblcart_productid' => $_GET['tblcart_productid'],'tblcart_quantity' => $_GET['tblcart_quantity']
            ));

        if($kontrol){
            Header("Location:../../front-cart.PHP?status=ok");

        }else{
            Header("Location:../../front-cart.PHP?status=no");
        }
    }

}

解决方法

您可以使用两种方法进行增量:

首先使用GET参数,当您绑定tblcart_quantity将+1添加到该值'tblcart_quantity'=> intval($ _ GET ['tblcart_quantity'])+ 1

 if (isset($_GET['key_cartitem_increase'])) {
    if($_GET['key_cartitem_increase']=="ok"){
         
        $cart_id=$_GET['tblcart_id'];

        $increaseitem=$db->prepare("UPDATE tbl_cart SET         
                        tblcart_userid=:tblcart_userid,tblcart_productid=:tblcart_productid,tblcart_quantity=:tblcart_quantity                    
                        WHERE tblcart_id=id");

        $kontrol=$increaseitem->execute(array(
            'id' => $_GET['tblcart_id'],'tblcart_userid' => $_GET['tblcart_userid'],'tblcart_productid' => $_GET['tblcart_productid'],'tblcart_quantity' => intval($_GET['tblcart_quantity']) + 1
            ));

        if($kontrol){
            Header("Location:../../front-cart.php?status=ok");

        }else{
            Header("Location:../../front-cart.php?status=no");
        }
    }

第二种方法,让我们的MySQL做一个增量,要做的就是编辑更新请求以编辑此 tblcart_quantity = tblcart_quantity + 1


 if (isset($_GET['key_cartitem_increase'])) {
    if($_GET['key_cartitem_increase']=="ok"){
         
        $cart_id=$_GET['tblcart_id'];

        $increaseitem=$db->prepare("UPDATE tbl_cart SET         
                        tblcart_userid=:tblcart_userid,tblcart_quantity=tblcart_quantity+1                    
                        WHERE tblcart_id=id");

        $kontrol=$increaseitem->execute(array(
            'id' => $_GET['tblcart_id'],'tblcart_quantity' => $_GET['tblcart_quantity']
            ));

        if($kontrol){
            Header("Location:../../front-cart.php?status=ok");

        }else{
            Header("Location:../../front-cart.php?status=no");
        }
    }