查询数据库(webcart)

时间:2017-11-28 17:35:38

标签: php mysql

我有这个问题,如果项目存在于购物车表中,它将更新数量,但如果我添加另一个不存在的项目,则它不起作用。如果购物车表是空的,它可以工作,简而言之,我怎么能插入另一个不存在的产品?提前谢谢。

if(!empty($_POST["quantity"])) {
    $productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
    $cart_item = $db_handle->runQuery("SELECT * FROM `cart`");

    if(!empty($cart_item)){
        $code = $_GET["code"];
        $query = "SELECT code FROM `cart` WHERE code='" . $_GET["code"] . "'";
        $quantityByCode = $db_handle->runQuery("SELECT * FROM cart WHERE code='" . $_GET["code"] . "'");
        $nQuan = $quantityByCode[0]["quantity"] + $_POST["quantity"];
        if($query = $code){
            $db_handle->runQ("UPDATE cart SET quantity = ".$nQuan." WHERE code='" . $_GET["code"] . "'");
        }else{
            $db_handle->runQ("INSERT INTO cart (`name`, `code`, `size`, `quantity`, `price`, `cond`)VALUES ('". $productByCode[0]["name"] ."', '". $productByCode[0]["code"] ."' ,'". $productByCode[0]["size"] ."',". $_POST["quantity"] .",". $productByCode[0]["price"] .",1)");
        }

    }else{
        $query="INSERT INTO cart (`name`, `code`, `size`, `quantity`, `price`, `cond`) VALUES ('". $productByCode[0]["name"] ."', '". $productByCode[0]["code"] ."' ,'". $productByCode[0]["size"] ."',". $_POST["quantity"] .",". $productByCode[0]["price"] .",1)";
                $is_query_successful=mysql_query($query);
    }
}

0 个答案:

没有答案