正在回答 回答被采纳积分+1
2回答
麦色瑟斯
2019-07-22 17:08:24
<?php require '../tools.func.php'; require '../db.func.php'; // 1. 接收商品 id $product_id = intval($_GET['product_id']); // 2. 接收数量 $quantity = intval($_GET['quantity']); if (empty($product_id) || empty($quantity)) { header('location: index.php'); exit; } $prefix = getDBPrefix(); $sql = "SELECT id, name, price, code, description FROM {$prefix}product WHERE id = '$product_id'"; $product = queryOne($sql); if(!empty(getSession('username','shop'))){ $uid=getSession('id','shop'); $sql="SELECT id,products,price,quantity,uid FROM {$prefix}cart WHERE uid='$uid'"; $cart=queryOne($sql); if (!empty($cart)) { // 更新记录 $products = json_decode($cart['products'], true); if (array_key_exists($product_id, $products)) { $products[$product_id]['quantity'] += $quantity; } else { $products[$product_id] = ['quantity' => $quantity, 'product' => $product]; } $products = addslashes(json_encode($products)); $cart_price = $cart['price'] + $quantity * $product['price']; $cart_quantity = $cart['quantity'] + $quantity; $sql = "UPDATE {$prefix}cart SET products = '$products', price = '$cart_price', quantity = '$cart_quantity' WHERE id = '{$cart['id']}'"; execute($sql); } else { // 生成 products, price, quantity, uid, created_at $products[$product_id] = ['quantity' => $quantity, 'product' => $product]; $price = $product['price'] * $quantity; $created_at = date('Y-m-d H:i:s'); // 添加新的数据 $products = addslashes(json_encode($products)); // \' \" $sql = "INSERT INTO {$prefix}cart(products, price, quantity, uid, created_at) VALUES('$products', '$price', '$quantity', '$uid', '$created_at')"; execute($sql); } header('location: cart.php'); // json [{2: {quantity: 4, product: {id: , price: }}}, {}] }else{ }
PHP小白零基础入门
- 参与学习 人
- 提交作业 626 份
- 解答问题 4930 个
想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星