Codeigniter将图像上传到路径文件夹但在数据库中没有记录

时间:2018-01-11 09:16:39

标签: php codeigniter codeigniter-3

我需要一些帮助,我可以将图像上传到文件夹路径但是在数据库中没有我插入图像的记录... 这是我的代码

----控制器代码----

    public function insert_product()
{
        $this->form_validation->set_rules('product_name','Productname','required');
        $this->form_validation->set_rules('product_price','Amount','required');
        $this->form_validation->set_rules('product_stock','Stock','required');
        $this->form_validation->set_rules('categorie_id','categorie_id','required');
        $this->form_validation->set_rules('product_description','Description','required');
        $config = array
        (
            'upload_path' => './assets/img',
            'allowed_types' => 'jpg|png|jpeg|bmp',
            'max_size'=> 0,
            'filename' => $_FILES['product_image']['name']
    );
    $this->load->library('upload',$config);
    if($this->upload->do_upload('product_image'))
    {
        $uploaddata  = $this->upload->data();
        $product_image=$uploaddata['file_name'];
        // $this->db->insert('product',array('product_image'=>$this->upload->file_name));
    }
    if ($this->form_validation->run()) 
    {
        $data = $this->input->post();
        unset($data['submit']);
        $this->load->model('queries_product');  
        if($this->queries_product->insert_product($data))
        {
            $this->session->set_flashdata('msg','Successfully Inserted');
        }
        else
        {
            $this->session->set_flashdata('msg','Failed to Insert');
        }
        return redirect('inventory');
    }
    else
    {
        echo validation_errors ();
    }

}

-----型号代码-------

    public function insert_product($data)
    {   
        return $this->db->insert('product',$data);

    }

}

2 个答案:

答案 0 :(得分:0)

  1. 首次验证表格
  2. 然后上传
  3. <form method="post" name="testForm" ng-controller="ExampleController" action="test.place">
      <input ng-model="val" name="anim"  />
             <button>submit</button>
    </form>
    

答案 1 :(得分:0)

像这样(就像我在评论中提到的那样)

if( false != ($valid = $this->form_validation->run()) && $this->upload->do_upload('product_image')){
    $uploaddata  = $this->upload->data();
    $product_image=$uploaddata['file_name'];
    // $this->db->insert('product',array('product_image'=>$this->upload->file_name));
    $data = $this->input->post();
    unset($data['submit']);
    $this->load->model('queries_product');  
    if($this->queries_product->insert_product($data)){
        $this->session->set_flashdata('msg','Successfully Inserted');
    }else{
        $this->session->set_flashdata('msg','Failed to Insert');
    }
    return redirect('inventory');
}else{
    if(!$valid)
       echo validation_errors();
    else
       $this->session->set_flashdata('msg','Failed to Upload');
}

通过在if条件中分配false != ( $valid = ...),我们可以检查哪一个实际上失败了,而没有做任何其他实际工作。

正如我在评论中提到的,if中参数的顺序很重要。如果先完成validation并返回false,PHP已经知道条件不会成功。那是因为&&要求双方都是true,如果一方是false,则不需要检查另一方,因此PHP会立即进入块中的下一个条件(这种情况下的else)没有上传。