2013-08-29 62 views
0

我爲我的數組創建了一個循環,然後是更新批處理,但每當我嘗試更新1項時,我的代碼都不會更新。我無法找到我的錯誤在哪裏。我怎樣才能以簡單的方式執行更新批處理?Codeigniter中的更新批處理錯誤

這裏是我的代碼:

/*THIS IS ALL ARRAY*/ 
$id = $this->input->post('idx'); 
$desc = $this->input->post('itemdesc'); 
$qty = $this->input->post('qty'); 
$price = $this->input->post('price'); 
$status = $this->input->post('status'); 

for($x = 0; $x < sizeof($id); $x++){ 

    $total[] = $price[$x] * $qty[$x]; 

    $updateArray[] = array(
     'poid' => $id[$x], 
     'item_desc' => $desc[$x], 
     'item_qty' => $qty[$x], 
     'price' => $price[$x], 
     'total' => $total[$x], 
     'status' => $status[$x] 
    ); 

    $this->db->update_batch('po_order_details',$updateArray, 'poid'); //I guess poid is my error but im not sure. I think my array won't find the correct id for where. 

    } 

這裏是我的陣列輸出樣本:

Array 
(
    [0] => Array 
     (
      [poid] => 5 
      [item_desc] => Yakisoba 
      [item_qty] => 15 
      [price] => 40,000.00 
      [total] => 600 
      [status] => ACTIVE 
     ) 

    [1] => Array 
     (
      [poid] => 6 
      [item_desc] => Laptop 
      [item_qty] => 5 
      [price] => 15,000.00 
      [total] => 75 
      [status] => ACTIVE 
     ) 

    [2] => Array 
     (
      [poid] => 7 
      [item_desc] => Speaker 
      [item_qty] => 3 
      [price] => 5,000.00 
      [total] => 15 
      [status] => ACTIVE 
     ) 

    [3] => Array 
     (
      [poid] => 8 
      [item_desc] => Mouse 
      [item_qty] => 5 
      [price] => 500.00 
      [total] => 2500 
      [status] => ACTIVE 
     ) 

    [4] => Array 
     (
      [poid] => 9 
      [item_desc] => Keyboard 
      [item_qty] => 5 
      [price] => 1,000.00 
      [total] => 5 
      [status] => ACTIVE 
     ) 

) 

這是所有球員,我希望你能幫助我。

+0

有什麼錯誤?哪一個不更新?將'DB_DEBUG'設置爲'TRUE',同時檢查實際的查詢。 – Red

+0

也看起來像你正在循環內更新它。您正在通過循環創建一個數組,因此您需要在循環完成後執行更新。 – Red

+0

每當我嘗試更新時,我都無法更新數據。 – Jerielle

回答

0

好吧,我做到了,但我的過程是漫長的

for ($x = 0; $x < sizeof($id); $x++) { 
    $total[] = $price[$x] * $qty[$x]; 
    $idvar = $desc[$x]; 

    $updateArray[] = array(
     'poid' => $id[$x], 'item_desc' => $desc[$x], 'item_qty' => $qty[$x], 
     'price' => $price[$x], 'total' => $total[$x], 'status' => $status[$x] 
    ); 
} 

for ($var = 0; $var < sizeof($updateArray); $var++) { 
    $idx = $updateArray[$var]['poid']; 

    $test = array(
     'item_desc' => $updateArray[$var]['item_desc'], 
     'item_qty' => $updateArray[$var]['item_qty'], 
     'item_price' => $updateArray[$var]['price'], 
     'total'  => $updateArray[$var]['total'], 
     'status'  => $updateArray[$var]['status'] 
    ); 

    $this->db->where('poid', $updateArray[$var]['poid']); 
    $this->db->update('po_order_details', $test); 
} 
1

嘗試用單引號括住每個數組的值,然後執行查詢。我已編輯代碼以將數組值包含在單引號中。請嘗試一下。 這裏是編輯的代碼。

$updateArray[] = array(
     'poid' => "'".$id[$x]."'", 
     'item_desc' => "'".$desc[$x]."'", 
     'item_qty' => "'".$qty[$x]."'", 
     'price' => "'".$price[$x]."'", 
     'total' => "'".$total[$x]."'", 
     'status' => "'".$status[$x]."'" 
    ); 
+0

好的,謝謝我會盡力回覆 – Jerielle

1

嘗試這樣可能會幫助您:

/*THIS IS ALL ARRAY*/ 
$id = $this->input->post('idx'); 
$desc = $this->input->post('itemdesc'); 
$qty = $this->input->post('qty'); 
$price = $this->input->post('price'); 
$status = $this->input->post('status'); 

for($x = 0; $x < sizeof($id); $x++){ 

$total[] = $price[$x] * $qty[$x]; 

$updateArray = array(
    //'poid' => $id[$x], 
    'item_desc' => $desc[$x], 
    'item_qty' => $qty[$x], 
    'price' => $price[$x], 
    'total' => $total[$x], 
    'status' => $status[$x] 
); 

$this->db->where('poid', $id[$x])->update('po_order_details', $updateArray); 
//$this->db->update_batch('po_order_details',$updateArray, 'poid'); //I guess poid is my error but im not sure. I think my array won't find the correct id for where. 

} 
+0

。我已經解決了它。但我會嘗試你的代碼。這似乎是正確的。 – Jerielle