简体   繁体   中英

Saving multiple image paths while inserting data in 2 tables in Codeigniter

I am trying to make this app work. At the moment it does upload multiple images to '[upload]' folder and data in one of the table (other than image folder).

I have 2 tables. One for other info and one images only. Other info table inserts even if there is an error. while image table doesn't insert anything.

My model is:

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();
}

Upload controller:

  $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 {

Controller that links to 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');
   }
}

And the FORM:

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

Please Help.

In your model there is a return statement given -

return $query;

So the remaining codes won't execute, that's why images related data is not inserting

 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();
}

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