使用codeigniter插入多个图像

时间:2017-06-16 02:46:42

标签: php mysql image codeigniter

我正在尝试上传多个图像,我会立即将数据插入多个表格。当用户填写表单时,首先会将一些数据插入inserted_id并将table B 插入<{strong>> $filesCount = count($_FILES['picture']['name']); //my input file name = 'picture' for ($i = 0; $i < $filesCount; $i++) { $_FILES['userFile']['name'] = $_FILES['picture']['name'][$i]; $_FILES['userFile']['type'] = $_FILES['picture']['type'][$i]; $_FILES['userFile']['tmp_name'] = $_FILES['picture']['tmp_name'][$i]; $_FILES['userFile']['error'] = $_FILES['picture']['error'][$i]; $_FILES['userFile']['size'] = $_FILES['picture']['size'][$i]; $config['upload_path'] = '/uploads/user/test/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 0; $new_name = uniqueId(); $config['file_name'] = $new_name; $this->load->library('upload', $config); if($this->upload->do_upload('userFile')){ $fileData = $this->upload->data(); $uploadData[$i]['file_name'] = $fileData['file_name']; } } if(!empty($uploadData)) { $this->Insert_model->setImage($uploadData); $isCreate = $this->Insert_model->createImage($uploadData); } 。这是我编码的样子。

控制器

public function createImage($data = array()){
    $this->db->trans_begin();

    $userInfo = array(
        'user_id'          => $this->getUserId(),     //UserId fetch from session
        'title'            => $this->getTitle(),
        'description'      => $this->getDescription(),
    );

    $this->db->insert('UserInfo', $userInfo);   //Data inserted to table UserInfo first

    $insert_id = $this->db->insert_id();        //And getting the inserted_id

    $data[] = array(
        'user_id'          => $this->getUserId(),
        'title_id'         => $insert_id,            //Insert Inserted id
        'image_name'       => $this->getImage(),
    );

    $this->db->insert_batch('UserImage', $data);     //Insert data to table UserImage

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
    }
    else
    {
        $this->db->trans_commit();
        return ($this->db->affected_rows() != 1) ? false : true;
    }

}

Insert_model

Array
(
    [0] => Array
        (
            [file_name] => 5943442cd1380.jpg
        )

    [1] => Array
        (
            [file_name] => 5943442cd1380.png
        )

    [2] => Array
        (
            [user_id] => 2
            [title_id] => 1
            [image_name] => Array
                (
                    [0] => Array
                        (
                            [file_name] => 5943442cd1380.jpg
                        )

                    [1] => Array
                        (
                            [file_name] => 5943442cd1380.png
                        )

                )

        )

)

插入表UserImage的数据输出

Array
(
    [0] => Array
        (
            [user_id] => 2
            [title_id] => 1
            [file_name] => 5943442cd1380.jpg
        )

    [1] => Array
        (
            [user_id] => 2
            [title_id] => 1
            [file_name] => 5943442cd1380.png
        )

)

使用输出,数据无法插入第二个表。

预期输出

{{1}}

1 个答案:

答案 0 :(得分:2)

您必须将此部分更改为。

public string Apple {get; set;}