Codeigniter 4 - 更新多行
Codeigniter 4 - update multiple rows
我想立即更新一些与我的图像相关的信息,但无法弄清楚我面临的问题。
这是我的看法:
<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>
这是我的控制器:
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']);
}
我的 print_r returns 我的数组,但无法更新。我有错误:
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
$builder->update() 与一维数组一起使用,而您的 $data 数组是多维的。所以你需要在for循环里面使用它;
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);
}
您也可以使用 $builder->updateBatch() 但随后您将需要重组 $data 数组。看看documentation.
我的问题的答案如下:感谢@mail2bapi 的一半解决方案:
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);
}
我想立即更新一些与我的图像相关的信息,但无法弄清楚我面临的问题。
这是我的看法:
<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>
这是我的控制器:
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']);
}
我的 print_r returns 我的数组,但无法更新。我有错误:
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
$builder->update() 与一维数组一起使用,而您的 $data 数组是多维的。所以你需要在for循环里面使用它;
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);
}
您也可以使用 $builder->updateBatch() 但随后您将需要重组 $data 数组。看看documentation.
我的问题的答案如下:感谢@mail2bapi 的一半解决方案:
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);
}