簡體   English   中英

codeigniter 中的多批更新不起作用

[英]multiple batch update in codeigniter not working

我有一個 codeigniter 網站,用戶可以在其中使用復選框 select 多個數據並一次編輯這些數據,一切正常,多行 cn 被選中,它們一次顯示,但是在我提交表單后,它沒有被保存,我做了以下事情:

 <label for="inputEmail4">Product Name</label> <input type="text" name="name[]" class="form-control" id="inputEmail4" value="<?=$valad->name?>" required> <input type="hidden" name="id[]" class="form-control" id="inputEmail4" value="<?=$valad->id?>" required> <label for="inputEmail4">SKU</label> <input type="text" name="sku[]" class="form-control" id="inputEmail4" value="<?=$valad->sku?>" required>

 if(isset($_POST['editinventoryproducts'])) { $id=$this->input->post('id'); $name=$this->input->post('name'); $sku=$this->input->post('sku'); $this->excel_import_model->editinventoryproductsm($id,$name,$sku); $this->session->set_flashdata("Successade","Product Edited Successfully;"), redirect('inventoryproducts'; 'refresh'); }

最后是 model:

 public function editinventoryproductsm($id,$name,$sku) { $this->db->where_in('id', $id); $this->db->update('inventoryproducts', array('name' => $name, 'sku' => $sku)); return true; }

我收到以下數據庫錯誤:

 Unknown column 'Array' in 'field list' UPDATE `inventoryproducts` SET `name` = Array, `sku` = Array WHERE `id` IN('16', '17')

誰能告訴我這里出了什么問題,在此先感謝

希望您在 model 中有正確的數組數據,如果是這樣,那么這可以通過update_batch輕松完成。

首先,您需要正確創建數組,然后$this->db->update_batch的單行就足以完成您的工作:

public function editinventoryproductsm($id,$name,$sku) {
    $i = 0;
    foreach ($id as $a){
        $data[$i] = array('id' => $id[$i], 'name' => $name[$i],'sku' => $sku[$i]);
        $i++;
    }

    $this->db->update_batch('inventoryproducts', $data, 'id');
    return true;
}

來自 CI 文檔:第一個參數將包含表名,第二個是值的關聯數組,第三個參數是 where 鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM