nojoud kaled
nojoud kaled

Reputation: 13

Insert multiple data in one single database row

I have code for uploading multiple images, it is returning the word "ARRAY", not return the content when I make " echo , die;", and in the other ways it return just the first upload and inserted into database.

I want to insert multiple value "IMGE" in single row database.

Controller:

function uploadFile(){

    $data = array();
    // If file upload form submitted
    if($this->input->post('Submit') && !empty($_FILES['files']['name']))
    {
        $filesCount = count($_FILES['files']['name']);
        for($i = 0; $i < $filesCount; $i++){
            $_FILES['file']['name']     = $_FILES['files']['name'][$i];
            $_FILES['file']['type']     = $_FILES['files']['type'][$i];
            $_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
            $_FILES['file']['error']     = $_FILES['files']['error'][$i];
            $_FILES['file']['size']     = $_FILES['files']['size'][$i];

            // File upload configuration
            $uploadPath = 'uploads/files/';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = 'jpg|jpeg|png|gif';

            // Load and initialize upload library
            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            // Upload file to server
            if($this->upload->do_upload('file')){
                // Uploaded file data

                ///////////////////////////////////////////
             //HERR I WANT TO RETURN THE  -  $file[$i]  - WITH ALL
             //THE IMAGE UPLAD TO INSER IN SINGLE ROW IN DATABASE
             ///////////////////////////////////////////    
                $fileData = $this->upload->data();
                $uploadData[$i]['file_name'] = $fileData['file_name'];
                $uploadData[$i]['uploaded_on'] = date("Y-m-d H:i:s");

                $file[$i] = $uploadData[$i]['file_name']; 
                return $file[$i];
                echo $file[$i]  , die; // here it is just return first image upload 


            }

        }

        if(!empty($uploadData)){

           return $uploadData;
           echo $new_array, die;// here it is  return word "ARRAY"}}

Model:

function insert_data($data) // from one table 
{  
      $insert = $this->db->insert('tbl_reservations', $data);
}

Upvotes: 0

Views: 299

Answers (3)

vils
vils

Reputation: 31

You can't insert an array o DB field, you have to loop throw it.

function insert_data($data) // from one table 
{  
    // I suppose that $data is multi dimensional array that conatins single 'row' on each item
    foreach($data as $item){
       $insert = $this->db->insert('tbl_reservations', $item);
    } 
}

EDIT 2

Ok now i figured out the problem. Tha data array passed to insert_data contains an array with all file names.

REPLACE your insert() whith the below code:

public function insert(){ 
    $uploadedFiles = $this->uploadFile(); // returns the file names array 

    // Map post data to table field
    $data = array(
       'visittype' => $this->input->post('visittype'),
       'date' => $this->input->post('date'),
       'time' => $this->input->post('time'),
       'reasons' => $this->input->post('reasons'),
       'symptoms' => $this->input->post('symptoms'),
       'doctor_id' => $this->input->post('doctorid'),
       'user_id' => $this->session->userdata('doctor')[0]->user_id
    );        

    // Loop on file names and insert each one on the db row.
    foreach ($uploadedFiles as $file){ 
        $data['file_name'] = $file['file_name'];
        $this->Reservation_model->insert_data($data); 
    }
} 

This way yuo will have as many rows as the uploaded file. If you want to have only one row that contains all files you have to store them as json string and decode when needed.

EDIT 3

uploadedFiles() returns something like this:

$uploadedFiles = [
    ['file_name' => 'file_one.jpg'],
    ['file_name' => 'file_two.jpg'],
    ['file_name' => 'file_three.jpg']
];

you can't store an array on a DB row field.

I can suggest you two ways:

1 - Create a Master/Detail structure. tbl_reservations to store master data, the current one and tbl_reservations_files to store file names for each reservation.

2 - Store file name as string, like CSV.

This is the code for solution 2:

public function insert(){
    $uploadedFiles = $this->uploadFile(); // returns the file names array

    // Map post data to table field
    $data = array(
        'visittype' => $this->input->post('visittype'),
        'date' => $this->input->post('date'),
        'time' => $this->input->post('time'),
        'reasons' => $this->input->post('reasons'),
        'symptoms' => $this->input->post('symptoms'),
        'doctor_id' => $this->input->post('doctorid'),
        'user_id' => $this->session->userdata('doctor')[0]->user_id
    );

    // Reduce file list as a CSV string
    $data['file_name'] = array_reduce($uploadedFiles, function ($carry, $item) { 
        // $carry is the value from previews iteration, $item is current array item. see array_reduce on php doc for more.
        // ',' can be replaced with any char of your need
        return $carry . ($carry === '' ?: ',') . $item['file_name'];
    }, '');

    // Insert data on DB
    $this->Reservation_model->insert_data($data);
} 

Upvotes: 0

JeffB
JeffB

Reputation: 206

Change

 echo $new_array, die;// here it is  return word "ARRAY"}}

To

print_r($new_array);

As $new_array is array, you need to print array using print_r or var_dump. Using echo will show Array as it is an array. Also don't use die, it will stop script and you won't be able to insert second image.

Upvotes: 0

sabrine abdelkebir
sabrine abdelkebir

Reputation: 83

The die function prints a message and exits the current script, for this reason only the first file is inserted .so remove die .

Upvotes: 1

Related Questions