如何通过在 Codeigniter 中更新现有图像路径(而不是替换)将更多图像添加到数据库


How to add more images to db by updating existing images path (not replacing) in Codeigniter

在这里寻求帮助。

我可以添加更多图像到上传文件夹以及图像路径到db,但这实际上替换了我想要保留的先前上传的图像(图像保留在上传文件夹中,但数据库中的图像路径发生了变化)。

我在数据库中现有的图像路径是内爆形式(图像1jpg,图像2.jpg,..,)

我的模型:

function add_more_images_to_listing($data, $id) {
  $data = array(
    'property_images' => $data['images'],
  );
  $this->db->where('property_ref_id', $id);
  $query=$this->db->update('vbc_property_images', $data);
  return $query;
}

控制器:

$id = $this->uri->segment(3);
$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
  if($_FILES['uploadedimages']['error'][$i] != 0) {
    $this->form_validation->set_message('fileupload_check', 'At least 1 image needs to be uploaded in jpeg, png or gif format only.');
    return FALSE;
  }
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/property-images';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name']      = 'property_image_1';
$config['max_size']      = '0';
$config['overwrite']     = FALSE;
for ($i = 0; $i < $number_of_files; $i++) {
  $_FILES['uploadedimage']['name'] = $files['name'][$i];
  $_FILES['uploadedimage']['type'] = $files['type'][$i];
  $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
  $_FILES['uploadedimage']['error'] = $files['error'][$i];
  $_FILES['uploadedimage']['size'] = $files['size'][$i];
  $this->upload->initialize($config);
  if ($this->upload->do_upload('uploadedimage', $i)) {
    $data['uploadedimage'] = $this->upload->data();
    $image_name[$i] = $data['uploadedimage']['file_name'];
    $data['images'] = implode(',',$image_name);
  } else {
    $this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
    return FALSE;
  }
}

谢谢。

我只是想通了。我在模型中添加了:

    $this->db->select('property_images');
    $this->db->from('vbc_property_images');
    $this->db->where('property_ref_id', $id);
    $query = $this->db->get();
    $query_result = $query->result_array();
    if (empty($query_result)) {
    return FALSE;
}
    elseif (count($query_result) > 1) {
    return 0;
}
    else{
    $rowId = $query_result[0]['property_images'];
    //return $rowId;    
}

并更换:

'property_images' => $data['images'],

'property_images' => $rowId.','.$data['images'],

现在似乎正在工作。