简体   繁体   English

codeigniter 中的多批更新不起作用

[英]multiple batch update in codeigniter not working

i have a codeigniter website where user can select multiple data using checkbox and edit those at once, all that is working fine, multiple row cn be selected, they are displayed at once, but after i submit the form, its not getting saved, i did the following:我有一个 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'); }

and finally model:最后是 model:

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

i am getting the following database error:我收到以下数据库错误:

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

can anyone please tell me what is wrong in here, thanks in advance谁能告诉我这里出了什么问题,在此先感谢

Hope you have correct array data in the model, if so, then this can easily be done by update_batch .希望您在 model 中有正确的数组数据,如果是这样,那么这可以通过update_batch轻松完成。

First you need to create the array properly, then the single line of $this->db->update_batch is enough to do your job:首先,您需要正确创建数组,然后$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;
}

From the CI Documentation: The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.来自 CI 文档:第一个参数将包含表名,第二个是值的关联数组,第三个参数是 where 键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM