php - 删除多个数据库行和具有相同标题的图像
问题描述
我正在尝试使用 Codeigniter 根据“slug”列删除多行。表单 DB 的所有行都被删除,但问题出在图像上。从根目录只删除第一个图像。其他行图像未删除。
这是我的控制器:
public function deletePost() {
$slug = $this->input->post('type');
$query = $this->db->query("Select * from posts_images where slug='$slug'");
$img = $query->row_array();
$imgPath = $img['image_path'];
if (is_file($imgPath)) {
foreach ($query as $deletRow) { // This foreach is not working
unlink($imgPath.$deletRow);
}
$this->db->where('slug', $slug)->delete('posts_images');
$this->session->set_flashdata('success', 'Posts Deleted Successfully');
redirect('dashboard/Cproducts/showData', 'refresh');
}
else {
$this->db->where('slug', $slug)->delete('posts_images');
$this->session->set_flashdata('success', 'Posts Deleted Successfully');
redirect('dashboard/Cposts/showData', 'refresh');
}
}
这是数据库截图:
在上面的屏幕截图中,第二行 Image 没有被删除。在从数据库中删除行之前,我已经应用了 foreach,但是 foreach 不起作用。任何帮助都感激不尽。
谢谢
解决方案
在“delete”方法之前调用“where”方法。
尝试这个:
$this->db->where('slug', $slug)->delete('posts_images');