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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?