我不能将多张照片与多行一起插入

时间:2017-07-18 03:14:14

标签: php mysql codeigniter

我无法将多张照片与多行一起插入。

我这里有输入字段:

<input name="u_code[]" required="required" style="margin:0px; ">
<input name="u_name[]" required="required" style="margin:0px; ">
<input name="u_address[]" required="required" style="margin:0px; ">
<input name="photo[]" required="required" style="margin:0px; ">

这是我的控制者:

function user_add()
{
if ($_POST) 
{
    $u_id =$this->input->post('u_id');
    $u_code =$this->input->post('u_code');
    $u_name =$this->input->post('u_name');
    $u_address = $this->input->post('u_address');

    $data = array();
    for ($i = 0; $i < count($this->input->post('u_id')); $i++)
    {

        $data[$i] = array(
            'u_id' => $u_id[$i],
            'u_code' => $u_code[$i],
            'u_name' => $u_name[$i],
            'u_address' => $u_address[$i],

        );
    }
        $insert = $this->user_model->user_add($data);
        echo json_encode(array("status" => TRUE));
}
}

我的问题是我不知道应该添加哪些确切的代码来上传多行中的照片。

请在这里查看照片: screenshot of input field 谢谢你的帮助......

2 个答案:

答案 0 :(得分:0)

试试这个,

function user_add()
{
    if ($_POST) {
        $u_id = $this->input->post('u_id');
        $u_code = $this->input->post('u_code');
        $u_name = $this->input->post('u_name');
        $u_address = $this->input->post('u_address');

        $data = array();
        for ($i = 0; $i < count($this->input->post('u_id')); $i++) {

            $data[$i] = array(
                'u_id' => $u_id[$i],
                'u_code' => $u_code[$i],
                'u_name' => $u_name[$i],
                'u_address' => $u_address[$i],

                );
            $insert = $this->user_model->user_add($data);
        }        
        echo json_encode(array("status" => true));
    }
}

答案 1 :(得分:0)

看看这段代码可能有助于您了解如何处理多个图像

$files = $_FILES;
$count = count($_FILES['uploadfile']['name']);
for($i=0; $i<$count; $i++)
{
    $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
    $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
    $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
    $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
    $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
    $this->upload->initialize($this->set_upload_options());//function defination below
    $this->upload->do_upload('uploadfile');
    $upload_data = $this->upload->data();
    $name_array[] = $upload_data['file_name'];
    $fileName = $upload_data['file_name'];
    $images[] = $fileName;
}
$fileName = $images;

$_FILE它是通过POST方法上传到当前脚本的关联项目数组。为了进一步查看helper link

这是一个在脚本的所有范围内都可用的自动变量

function set_upload_options()
{ 
// upload an image options
     $config = array();
     $config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
     $config['remove_spaces']=TRUE;
     $config['encrypt_name'] = TRUE; // for encrypting the name
     $config['allowed_types'] = 'gif|jpg|png';
     $config['max_size'] = '78000';
     $config['overwrite'] = FALSE;
     return $config;
}

一些基本提示: 输入元素必须包含multiple=" multiple"或多个.Load upload库。

$this->upload->do_upload()会将在给定字段名称中选择的文件上传到目标文件夹。

$this->upload->data()返回与上传文件相关的数据数组,如文件名,路径,大小等。