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

时间:2018-09-17 12:34:14

标签: php

我有用于上传多个图像的代码,它返回单词"ARRAY",而不是在我制作" echo , die;"时返回内容,而以其他方式,它仅返回第一次上传并插入数据库中

我想在单行数据库中插入多个值"IMGE"

控制器:

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"}}

型号:

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

3 个答案:

答案 0 :(得分:1)

die函数将显示一条消息并退出当前脚本,因此仅插入第一个文件。因此请删除die。

答案 1 :(得分:0)

更改

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

收件人

print_r($new_array);

由于$new_array是数组,因此需要使用print_rvar_dump打印数组。使用echo将显示Array,因为它是一个数组。同样不要使用die,它将停止脚本,并且您将无法插入第二张图像。

答案 2 :(得分:0)

您不能在DB字段中插入数组,必须循环抛出该数组。

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);
    } 
}

编辑2

好,现在我知道了问题所在。传递给insert_data的数据数组包含具有所有文件名的数组。

使用以下代码替换您的insert()

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); 
    }
} 

这样,yuo将具有与上载文件一样多的行。如果只想包含所有文件的一行,则必须将它们存储为json字符串,并在需要时进行解码。

编辑3

uploadedFiles()返回如下内容:

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

您不能在数据库行字段上存储数组。

我可以建议您两种方式:

1-创建一个主/明细结构。 tbl_reservations用于存储主数据,当前数据和tbl_reservations_files用于存储每个保留的文件名。

2-将文件名存储为字符串,例如CSV。

这是解决方案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);
}