duanli0119 2018-09-17 12:34
浏览 36
已采纳

在单个数据库行中插入多个数据

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);
}
  • 写回答

3条回答 默认 最新

  • dtudj42064 2018-09-17 13:22
    关注

    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);
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dpdrtj1075 2018-09-17 12:39
    关注

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

    评论
  • dro44817 2018-09-17 12:40
    关注

    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.

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 电脑锁屏时vba运行SAP,excel数据不能复制到SAP
  • ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
  • ¥30 各位help写一下代码
  • ¥15 在运行SDEdit模型下载不了
  • ¥15 求51控制l298n驱动的小车中超声波避障怎么写
  • ¥15 电脑连上WIFI却用不了
  • ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
  • ¥15 hyper-v出现的问题
  • ¥15 有能用的可加酬金,求可以批量下载懒人听书的软件,能登录自己帐号的。
  • ¥100 高博一起做RGB-D SLAM(5)VO无法出visualisation问题