简体   繁体   中英

Codeigniter 4 - update multiple rows

I would like to update some informations related to my images at once but can't figure out the problem I am facing.

This is my view:

<form action="my_controller_path" method="post">
    <?php foreach ($my_images as $mi) { ?>
        <div>
            <div class="img">
                <img src="<?php echo base_url($mi->image_path); ?>" />
            </div>

        <div class="other-infos">
            <div class="title">Image Text:</div>

                <input type="text" class="form-input" name="image_text[]" value="<?php echo $mi->image_info;?>"/>

            <div class="title">Order</div>
               <input type="number" value='<?php echo $mi->image_order; ?>' name="image_order[]">

            <div class="title">Color</div> 
                <input type="text" value='<?php echo $mi->image_color; ?>' name="image_color[]">

           <input type="hidden" name="image_id[]" class="image_id" value="<?php echo $mi->image_id;?>">
    </div>
<?php } ?>

<button>Update</button>

</form>

This is my Controller:

    public function update_image_infos(){
  
   $image_id = $this->request->getVar('image_id');
    
   $number = count($image_id);

    $db      = \Config\Database::connect();
    $builder = $db->table('product_images');

$data  = [];
    
        $image_text = $this->request->getVar('image_text');
        $image_color = $this->request->getVar('image_color');
        $image_order = $this->request->getVar('image_order');
        
        for($i=0; $i<$number; $i++){
            
        $data[] = [
                'image_text' => $image_text[$i],
                'image_color' => $image_color[$i],
                'image_order' => $image_order[$i]
            ];
    }

//print_r($data);

$builder->where('image_id',$image_id);
$builder->update($data);

etc. etc.   return redirect()->to($_SERVER['HTTP_REFERER']);

}

My print_r returns me the array, but can't update. I'm having error:

mysqli_sql_exception #1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0 = ('Info A','green','2'), 1 = ('Other Info','brown','1') WHERE `image_id...' at line 1

The answer to my problem is below: Thanks @mail2bapi for the half solution:

for($i=0; $i<$number; $i++){

$data = [
        'image_text' => $image_text[$i],
        'image_color' => $image_color[$i],
        'image_order' => $image_order[$i]
    ];

$builder->where('image_id',$image_id[$i]);  //this line is the answer
$builder->update($data);

}

$builder->update() is used with one dimensional array and your $data array is multidimensional. So you need to use it inside for loop;

for($i=0; $i<$number; $i++){
    
    $data = [
            'image_text' => $image_text[$i],
            'image_color' => $image_color[$i],
            'image_order' => $image_order[$i]
        ];

    $builder->where('image_id',$image_id);
    $builder->update($data);
}

You can also use $builder->updateBatch() but then you will need to restructure you $data array. Take a look at documentation .

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