1

I am trying to upload multiple images where I do inserting data to multiple table at once. When user filling up the form, some data will be inserted into table A first and getting the inserted_id to be inserted into table B. Here is what my coding looked like.

controller

$filesCount = count($_FILES['picture']['name']);      //my input file name = 'picture'

for ($i = 0; $i < $filesCount; $i++) {

    $_FILES['userFile']['name'] = $_FILES['picture']['name'][$i];
    $_FILES['userFile']['type'] = $_FILES['picture']['type'][$i];
    $_FILES['userFile']['tmp_name'] = $_FILES['picture']['tmp_name'][$i];
    $_FILES['userFile']['error'] = $_FILES['picture']['error'][$i];
    $_FILES['userFile']['size'] = $_FILES['picture']['size'][$i];

    $config['upload_path']          = '/uploads/user/test/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 0;

    $new_name = uniqueId();
    $config['file_name'] = $new_name;

    $this->load->library('upload', $config);

    if($this->upload->do_upload('userFile')){
        $fileData = $this->upload->data();
        $uploadData[$i]['file_name'] = $fileData['file_name'];
    }
}


if(!empty($uploadData)) {
    $this->Insert_model->setImage($uploadData);

    $isCreate = $this->Insert_model->createImage($uploadData);

}

Insert_model

public function createImage($data = array()){
    $this->db->trans_begin();

    $userInfo = array(
        'user_id'          => $this->getUserId(),     //UserId fetch from session
        'title'            => $this->getTitle(),
        'description'      => $this->getDescription(),
    );

    $this->db->insert('UserInfo', $userInfo);   //Data inserted to table UserInfo first

    $insert_id = $this->db->insert_id();        //And getting the inserted_id

    $data[] = array(
        'user_id'          => $this->getUserId(),
        'title_id'         => $insert_id,            //Insert Inserted id
        'image_name'       => $this->getImage(),
    );

    $this->db->insert_batch('UserImage', $data);     //Insert data to table UserImage

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
    }
    else
    {
        $this->db->trans_commit();
        return ($this->db->affected_rows() != 1) ? false : true;
    }

}

Output of data that inserting into table UserImage

Array
(
    [0] => Array
        (
            [file_name] => 5943442cd1380.jpg
        )

    [1] => Array
        (
            [file_name] => 5943442cd1380.png
        )

    [2] => Array
        (
            [user_id] => 2
            [title_id] => 1
            [image_name] => Array
                (
                    [0] => Array
                        (
                            [file_name] => 5943442cd1380.jpg
                        )

                    [1] => Array
                        (
                            [file_name] => 5943442cd1380.png
                        )

                )

        )

)

With the output, data unable to be inserted into second table.

Expected output would be

Array
(
    [0] => Array
        (
            [user_id] => 2
            [title_id] => 1
            [file_name] => 5943442cd1380.jpg
        )

    [1] => Array
        (
            [user_id] => 2
            [title_id] => 1
            [file_name] => 5943442cd1380.png
        )

)
3
  • this is because, in model, your parameter is $data. And you are inserting only file name to it in controller and passing to model. Commented Jun 16, 2017 at 3:23
  • how am I going to get the inserted_id from controller? Commented Jun 16, 2017 at 3:29
  • have you tried that in multiple images if any image is not proper or not fulfill the criteria then what happens? Commented Jun 17, 2017 at 18:36

1 Answer 1

2

You have to change this part to.

$data[] = array(
    'user_id'          => $this->getUserId(),
    'title_id'         => $insert_id,            //Insert Inserted id
    'image_name'       => $this->getImage(),
);


foreach($data as $row) { // here data is from parameter
    $data1[] = array(
      'user_id'          => $this->getUserId(),
      'title_id'         => $insert_id,            //Insert Inserted id
      'image_name'       => $row['file_name'] // this line is changed
    );
}

$this->db->insert_batch('UserImage', $data1);// variable name is changed. 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the solution. I manage to get the correct output but I am getting error Undefined index: file_name on line 'image_name' => $row['file_name']
Oh I just did some mistake in my coding. Your solution working perfectly. Thank you!
I have a question that what if the user uploaded several images and among that one image is not fulfilling the criteria or not even an image, in that condition what should I do?
@ankitsuthar, you can validate your inputs using javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.