Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading