如何在Codeigniter中使用jQuery Ajax更新表单数据而无需再次上传图像?

时间:2019-01-22 07:05:42

标签: jquery codeigniter

视图:

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            company_name = $("#company_name").val();
            var fileInputs = $('#company_logo');
            var formData = new FormData();
            $.each(fileInputs, function(i,fileInput){
                if( fileInput.files.length > 0 )
                {
                    $.each(fileInput.files, function(k,file){
                        formData.append('company_logo[]', file);
                    });
                }
            });
            formData.append('company_name', company_name);
            $.ajax({
                type:"POST",
                data:formData,
                processData: false,
                contentType: false,
                url:"<?php echo base_url(); ?>company_details",
                success:function(data){
                    $("#success_company").html(data);
                }
            });
        });
    });
</script>

控制器:

public function company_details()
{
    $this->db->select('company_name');
    $this->db->from('detail');
    $where = "company_name='".$this->input->post('company_name')."'";
    $this->db->where($where);
    $q = $this->db->get();
    if($q->num_rows() > 0)
    { 
        $dataInfo = array();
        $files = $_FILES;
        if($files!='')
        {
            $cpt = count($_FILES['company_logo']['name']);
            for($i=0; $i<$cpt; $i++)
            {           
                $_FILES['company_logo']['name']= $files['company_logo']['name'][$i];
                $_FILES['company_logo']['type']= $files['company_logo']['type'][$i];
                $_FILES['company_logo']['tmp_name']= $files['company_logo']['tmp_name'][$i];
                $_FILES['company_logo']['error']= $files['company_logo']['error'][$i];
                $_FILES['company_logo']['size']= $files['company_logo']['size'][$i];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload('company_logo');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;
            }

            $fileName = $images;
            $data = array(
                'company_name' => $this->input->post('company_name'),
                'company_logo' => implode(",",$fileName),
            );
            $where = "candidate_id='".$this->input->post('cid')."'";
            $this->db->where($where);
            $sql = $this->db->update('detail',$data);
            if($sql == true)
            {
                echo '<p>Company Detail Added</p>';
            }
            else
            {
                echo '<p>Unable to Proceed!</p>';
            }
        }
        else
        {
            $cpt = count($_FILES['company_logo']['name']);
            for($i=0; $i<$cpt; $i++)
            {           
                $_FILES['company_logo']['name']= $files['company_logo']['name'][$i];
                $_FILES['company_logo']['type']= $files['company_logo']['type'][$i];
                $_FILES['company_logo']['tmp_name']= $files['company_logo']['tmp_name'][$i];
                $_FILES['company_logo']['error']= $files['company_logo']['error'][$i];
                $_FILES['company_logo']['size']= $files['company_logo']['size'][$i];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload('company_logo');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;
            }

            $fileName = $images;
            $data = array(
                'company_name' => $this->input->post('company_name'),
            );
            $where = "candidate_id='".$this->input->post('cid')."'";
            $this->db->where($where);
            $sql = $this->db->update('detail',$data);
            if($sql == true)
            {
                echo '<p>Company Detail Added</p>';
            }
            else
            {
                echo '<p>Unable to Proceed!</p>';
            }
        }
    }
    else
    {
        $dataInfo = array();
        $files = $_FILES;
        $cpt = count($_FILES['company_logo']['name']);
        for($i=0; $i<$cpt; $i++)
        {           
            $_FILES['company_logo']['name']= $files['company_logo']['name'][$i];
            $_FILES['company_logo']['type']= $files['company_logo']['type'][$i];
            $_FILES['company_logo']['tmp_name']= $files['company_logo']['tmp_name'][$i];
            $_FILES['company_logo']['error']= $files['company_logo']['error'][$i];
            $_FILES['company_logo']['size']= $files['company_logo']['size'][$i];
            $this->upload->initialize($this->set_upload_options());
            $this->upload->do_upload('company_logo');
            $upload_data = $this->upload->data();
            $name_array[] = $upload_data['file_name'];
            $fileName = $upload_data['file_name'];
            $images[] = $fileName;
        }

        $fileName = $images;
        $data = array(
            'company_name' => $this->input->post('company_name'),
            'company_logo' => implode(",",$fileName)
        );
        $sql = $this->db->insert('company_detail',$data);
        if($sql == true)
        {
            echo '<p>Company Detail Added</p>';
        }
        else
        {
            echo '<p>Unable to Proceed!</p>';
        }
    }
}

在此代码中,我通过单击提交按钮上传多个图像。现在,当我第一次上传company_logo然后成功将数据插入数据库时​​会发生什么。但是当我更新信息时。未经更改的company_logo图片会显示错误,即

第一个错误:

Message: Undefined index: company_logo

第二次错误:

Message: count(): Parameter must be an array or an object that implements Countable

那么,如何解决此问题?请帮助我

谢谢

1 个答案:

答案 0 :(得分:0)

首先在if部分中删除

 if($files!='') // put here if(count($files) > 0)

并删除没有使用的其他部分中的图片上传代码。