更新具有不同数量的相同ID(仅更新最先出现的ID)

时间:2019-02-20 21:42:23

标签: php mysql updates inventory-management

我是数据库新手,正在开发库存管理,我有四个表,两个表分别用于purchasepurchase_detail。还有另外两个salesale_detail

此图片是我的purchase_detailenter image description here 这是插入新购买的代码,效果很好。

$sql_insert_sale = "INSERT INTO 
purchase(recipt_no,date,total,vat,totalwithvat) VALUES ( ?, ?, ? ,?, 
?)";
            $query = $dbh->prepare($sql_insert_sale);
            $query->bindParam(1, $recipt_no, PDO::PARAM_STR);
            $query->bindParam(2, $date, PDO::PARAM_STR);
            $query->bindParam(3, $price_all, PDO::PARAM_STR);
            $query->bindParam(4, $vat_all, PDO::PARAM_STR);
            $query->bindParam(5, $total_all, PDO::PARAM_STR);
            $query->execute();
            $lastInsertId = $dbh->lastInsertId();
            if ($lastInsertId) {
            $sql_insert_purchase = "INSERT INTO 
 purchase_detail(purchase_id, product_id,vat,price,quantity,amount) "
             . "VALUES ( :purchase_id 
,:product_id,:vat,:price,:quantity,:amount)";
                for($i=0;$i<count($partnumber);$i++) {
                    $data=array(
                                 ':purchase_id'=> $lastInsertId,
                                 ':product_id'=> $id[$i],
                                 ':vat'=> $vat[$i], 
                                 ':price'=> $amount[$i],
                                 ':quantity'=> $quantity[$i],
                                 ':amount'=> $total[$i]
                                );
                     $statement = $dbh->prepare($sql_insert_purchase);
                     if($statement->execute($data)){
                         echo "Successfull";
                     }else{
                         print_r($dbh->errorInfo());
                     }

这是我的sale_detailenter image description here

在更新sale表中的quantity时,purchase_detail表存在问题。如您所见,我们有相同数量不同的产品,当我进行新的销售时,它总是用排在最前面的product_id更新行中的唯一数量。以下是进行新交易的代码

$sql_insert_sale = "INSERT INTO 
  sale(recipt_no,cus_name,date,total)VALUES ( ?,?, ?, ? )";
        $query = $dbh->prepare($sql_insert_sale);
        $query->bindParam(1, $recipt_no, PDO::PARAM_STR);
        $query->bindParam(2, $cus_name, PDO::PARAM_STR);
        $query->bindParam(3, $date, PDO::PARAM_STR);
        $query->bindParam(4, $total_all, PDO::PARAM_STR);
                $query->execute();
                $lastInsertId = $dbh->lastInsertId();
                if ($lastInsertId) {
                    $sql_insert_sale = "INSERT INTO 
 sale_detail(sale_id, product_id,vat,price_vat,quantity,amount) "
 . "VALUES ( :sale_id,:product_id,:vat,:price_vat,:quantity,:amount)";
                 for($i=0;$i<count($partnumber);$i++) {
                    $data=array(
                                 ':sale_id'=> $lastInsertId,
                                 ':product_id'=> $id[$i],
                                 ':vat'=> $vat[$i], 
                                 ':price_vat'=> $price[$i],
                                 ':quantity'=> $quantity[$i],
                                 ':amount'=> $total[$i]
                                );
                     $statement = $dbh->prepare($sql_insert_sale);
                     if($statement->execute($data)){
                         $sql_update_purchase="UPDATE purchase_detail 
                     INNER JOIN product "
                                 . 
                  "on(purchase_detail.product_id=product.product_id) "
                                 . "SET 
      purchase_detail.quantity=purchase_detail.quantity-$quantity[$i]"
                                 . " WHERE 
    purchase_detail.product_id=$id[$i] and product.product_id=$id[$i]";
    $statement_update=$dbh->prepare($sql_update_purchase);
                         if($statement_update->execute())

请告诉我代码有什么问题,非常感谢您的帮助。我快要完工了。

0 个答案:

没有答案