简体   繁体   中英

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:

 <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:

 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 .

First you need to create the array properly, then the single line of $this->db->update_batch is enough to do your job:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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