如何使用Codeigniter插入和移动多个图像?

时间:2019-01-02 06:18:10

标签: php jquery codeigniter mysqli upload

视图:

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            product_name = $("#product_name").val();
            color = $("#colorWell").val();
            $.ajax({
                type:"POST",
                data:{"product_name":product_name, "color":color},
                url:"<?php echo base_url(); ?>admin/products",
                success:function(data){
                    alert(data);
                }
            });
        });
    });
</script>

<input type="text" class="form-control" id="product_name" name="product_name">
<input type="color" id="colorWell" name="color">
<input type="file" id="product_image" name="product_image[]" multiple>
<input type="submit" class="btn btn-primary" id="submit" name="submit">

控制器:

public function products()
{
    $product_name = $this->input->post('product_name');
    $color = $this->input->post('color');

    $dataInfo = array();
    $files = $_FILES;
    $cpt = count($_FILES['product_image']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['product_image']['name']= $files['product_image']['name'][$i];
        $_FILES['product_image']['type']= $files['product_image']['type'][$i];
        $_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
        $_FILES['product_image']['error']= $files['product_image']['error'][$i];
        $_FILES['product_image']['size']= $files['product_image']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        $this->upload->do_upload();
        $dataInfo[] = $this->upload->data();
    }
    $data = array(
            'product_name' => $product_name,
            'color' => $color,
            'product_image' => implode(",",$dataInfo['product_image']),
        );
    $result_set = $this->db->insert('add_product',$data);
    if($sql == true)
    {
        echo 'New Product Added';
    }
    else
    {
        echo 'Unable to Proceed!';
    }
}

private function set_upload_options()
{   
    $config = array();
    $config['upload_path'] = ''.base_url().'resource/product/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;
    return $config;
}

在此代码中,我试图插入并且想要将图像移到文件夹中。但是现在的问题是,当我单击“提交”按钮时,它会引发如下所示的错误:

Message: Undefined index: product_image

查询外观:

INSERT INTO `product` (`product_name`, `color`, `product_image`) VALUES ('men hoodies','#004080', NULL)

我不知道我在哪里做错了。那么,我该如何解决这个问题?请帮我。

谢谢

3 个答案:

答案 0 :(得分:1)

将所有formdata和文件一起发送到ajax中。

这样的HTML代码...

<form method="POST" id="YourFormID" enctype="multipart/form-data">
<input type="text" class="form-control" id="product_name" name="product_name">
<input type="color" id="colorWell" name="color">
<input type="file" id="product_image" name="product_image[]" multiple>
<input type="submit" class="btn btn-primary" id="submit" name="submit">
</form>

此处是Ajax代码。...

<script type="text/javascript">
$(document).ready(function(){
    $("#submit").click(function(e){
        e.preventDefault();
        var formData = new FormData($('form#YourFormID')[0]);
        $.ajax({
            type:"POST",
            data:formData,
            url:"<?php echo base_url(); ?>admin/products",
            success:function(data){
                alert(data);
            }
        });
    });
});
</script>

您没有在ajax请求中发送文件。因此找不到索引product_image

答案 1 :(得分:0)

使用如下所示的array_column添加以获取所有product_image值

implode(",",array_column($dataInfo, 'product_image'))

答案 2 :(得分:0)

您没有提交文件数据。 使用CREATE TABLE IF NOT EXISTS `ci_sessions` ( `id` varchar(40) NOT NULL, `ip_address` varchar(45) NOT NULL, `timestamp` int(10) unsigned DEFAULT 0 NOT NULL, `data` blob NOT NULL, PRIMARY KEY (id), KEY `ci_sessions_timestamp` (`timestamp`) ); 上传文件数据,并将要添加的其他任何输入追加到formData

formData