我希望在此代码中,在插入时增加相同项目的数量而不向表中添加新记录

时间:2018-03-31 11:47:55

标签: php pdo

我希望在此代码中插入
时增加相同项目的数量    不向表中添加新记录。    插入工作已完成,但在插入表格时我需要   包含一个唯一数量的发票,以增加没有的项目数   重复记录。

   <?php
    session_start();
    include('../connect.php');
    $a = $_POST['invoice'];
    $b = $_POST['product_id'];
    $c = $_POST['qty'];
    $saleproduct = $_POST['saleproduct'];
    $w = $_POST['pt'];
    $amount = $_POST['amount'];
    $dateok = $_POST['dateok'];
    $result = $db->prepare("SELECT * FROM products WHERE product_id= :userid");
    $result->bindParam(':userid', $b);
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++){
    $sellprice=$row['price'];
    $prodcode=$row['prod_code'];
    $code=$row['product_code'];
    $color=$row['color_name'];
    $size=$row['size_name'];
        $sizenum=$row['note'];
    $name=$row['product_name'];
    }

    $sql = "UPDATE products 
            SET qty=qty-?
            WHERE product_id=?";
    $q = $db->prepare($sql);
    $q->execute(array($c,$b));
    $grandtotal = $sellprice- $saleproduct ;
    $d=$grandtotal*$c;
    $sql = "INSERT INTO sales_order 


(invoice_number,prod_code,product_id,qty,amount,saleproduct,name,color_name,size_name,note,price,product_code,date,dateok) VALUES 
    (:a,:prodcode,:b,:c,:d,:saleproduct,:e,:ee,:eee,:sizenum,:sellp,:i,:k,:kok)";
    $q = $db->prepare($sql);
    $q->execute(array(':a'=>$a,':prodcode'=>$prodcode,':b'=>$b,':c'=>$c,':d'=>$d,':saleproduct'=>$saleproduct,':e'=>$name,':ee'=>$color,':eee'=>$size,':sizenum'=>$sizenum,':sellp'=>$sellprice,':i'=>$code,':k'=>$date,':kok'=>$dateok));
    ?>

1 个答案:

答案 0 :(得分:0)

您需要运行UPDATE查询来更新现有记录的数量,即:

UPDATE `sales_order` SET `qty` = `qty` + 1 WHERE `id` = ?

您显然必须在id表中绑定所需记录的sales_order