2017-07-16 18 views
-1

それぞれに対して販売注文を更新中にエラーが発生する。データベースに既に存在する場合は既存の配列レコードを更新するelse配列を使用してデータベースに挿入する

foreach($_POST['productId'] as $key => $val){ 
      $invoice_arrs = array(
        "product_name"  =>  $_POST['productname'][$key], 
        "purchaseQty"  =>  $_POST['purchaseQty'][$key], 
        "sellingPrice"  =>  $_POST['sellingPrice'][$key], 
        "discount"   =>  $_POST['discount'][$key], 
        "dis_type"   =>  $_POST['discounttype'][$key], 
        "total"    =>  $_POST['total'][$key], 
        "net_price"   =>  $_POST['net_price'][$key] 
      ); 
$temp=UpdateRecords("sales_order","sales_order_id=$id",$invoice_arrs,1); 

    //here is my update record function 

    function UpdateRecords($table, $condition, $updata, $debug = "") { 
    global $conn; 

    foreach ($updata as $key => $value) { 
     if ($value != "now()") { 
      $fv [] = "$key = \"" . "$value" . "\""; 
     } else { 
      $fv [] = "$key = " . "$value" . ""; 
     } 
    } 

    $fv_list = trim (implode (", ", $fv)); 
    $query = "UPDATE $table SET " . "$fv_list" . " WHERE $condition"; 
    if ($debug == 1) { 
     echo $query; 
    } 

    $result = executeQuery($query); 

    if (! mysqli_affected_rows ($conn)) { 
     global $errormessage; 
     $errormessage = mysqli_error ($conn); 

     if (! empty ($errormessage)) 
      return 0; 
    } 

    return 1; 
} 
+1

を – Daniel

+0

詳細は、それを確認してください更新され、あなたの 'UpdateRecords'関数の定義を記載してください –

答えて

0

クエリの下に試すことができます -

INSERT INTO $table (product_name, 
        purchaseQty, 
        sellingPrice, 
        discount, 
        dis_type, 
        total, 
        net_price) 
VALUES($_POST['productname'][$key], 
     $_POST['purchaseQty'][$key], 
     $_POST['sellingPrice'][$key], 
     $_POST['discount'][$key], 
     $_POST['discounttype'][$key], 
     $_POST['total'][$key], 
     $_POST['net_price'][$key]) 
ON DUPLICATE KEY UPDATE product_name = VALUES($_POST['productname'][$key]), 
         purchaseQty = VALUES($_POST['purchaseQty'][$key]), 
         sellingPrice = VALUES($_POST['sellingPrice'][$key]), 
         discount = VALUES($_POST['discount'][$key]), 
         dis_type = VALUES($_POST['discounttype'][$key]), 
         total = VALUES($_POST['total'][$key]), 
         net_price = VALUES($_POST['net_price'][$key]); 
関連する問題