Home > Mobile >  multiple batch update in codeigniter not working
multiple batch update in codeigniter not working

Time:12-16

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[]"  id="inputEmail4" value="<?=$valad->name?>" required>
<input type="hidden" name="id[]"  id="inputEmail4" value="<?=$valad->id?>" required>

<label for="inputEmail4">SKU</label>
<input type="text" name="sku[]"  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

CodePudding user response:

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.

  • Related