在Codeigniter中的2个表中插入数据时保存多个图像路径

时间:2016-01-07 04:09:39

标签: php codeigniter codeigniter-3

我正在努力让这款应用运行起来。目前,它确实将多张图片上传到了[上传]'其中一个表中的文件夹和数据(图像文件夹除外)。

我有2张桌子。一个用于其他信息,仅一个图像。即使出现错误,也会插入其他信息表。而图像表不会插入任何内容。

我的模特是:

function insert_property_details($data) {
$this->db->trans_start();
// First Table
       $insert_property_in_database=array(
       'v_item_title'   => $this->input->post('v_item_title'),
       'v_item_description' => $this->input->post('v_item_description'),
       'images_reference_id'    => $this->db->insert_id(),
       );
       $query=$this->db->insert('vbc_vacation_item_attri',$insert_property_in_database);
        return $query;
// Second Table
        $data = array('property_images' => $data['uploadedimage']);
        $this->db->insert('vbc_property_images', $data);

        $this->db->trans_complete();
        return $this->db->insert_id();
}

上传控制器:

  $this->upload->initialize($config);
  if ($this->upload->do_upload('uploadedimage')) {
    $data['upload_data'] = $this->upload->data();
    $image_name = $data['upload_data']['file_name'];

    $data['upload_data'] = $image_name;
    $this->load->model('admin/model_users');
    $this->model_users->insert_property_details($data);
  } else {

链接到FORM的控制器:

public function post_property() {
//Some validation code here
if($this->form_validation->run() == FALSE) {
        redirect('dashboard/add-new-listing');
} else {
        $this->load->model('admin/model_users');
        if($query = $this->model_users->insert_property_details()) {
        redirect('dashboard/property-successfully-posted');
   }
}

表格:

<input type="file" class="input-text-custom" data-placeholder="select image/s" name="uploadedimages[]" accept="image/*" multiple id="file" />

请帮助。

1 个答案:

答案 0 :(得分:0)

在您的模型中,有一个return语句 -

return $query;

因此剩余的代码将不会执行,这就是图像相关数据未插入的原因

 function insert_property_details($data) {
$this->db->trans_start();
// First Table
       $insert_property_in_database=array(
       'v_item_title'   => $this->input->post('v_item_title'),
       'v_item_description' => $this->input->post('v_item_description'),
       'images_reference_id'    => $this->db->insert_id(),
       );
       $query=$this->db->insert('vbc_vacation_item_attri',$insert_property_in_database);
       // return $query;  this is not required
// Second Table
        $data = array('property_images' => $data['uploadedimage']);
        $this->db->insert('vbc_property_images', $data);

        $this->db->trans_complete();
        return $this->db->insert_id();
}