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);
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况