【问题标题】:Codeigniter|: error inserting image into mysqlCodeigniter|:将图像插入 mysql 时出错
【发布时间】:2018-01-14 01:11:44
【问题描述】:

每次我将图像插入数据库时​​都会出现此错误

错误号:1452

无法添加或更新子行:外键约束失败 (herbalcebu.product,约束product_ibfk_1外键 (categorie_id) 参考资料categorie (categorie_id))

插入product (product_image) 值('q3.jpg')

文件名:C:/xampp/htdocs/HerbalCebu/system/database/DB_driver.php

行号:691

这是我的控制器代码

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);

    }

【问题讨论】:

标签: php codeigniter


【解决方案1】:

如果上传图片,您的代码将插入图片,如果表单验证,则插入整个数据。

您的产品表有一个categorie_id 字段约束,它不能为空并且应该存在于类别表中,所以您得到了上面的错误。

您应该将product_image 数据与整个产品数据合并,方法是添加条件如果已上传添加额外上传的图像数据:

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');

        if ($this->form_validation->run()) 
        {
               $data = $this->input->post();
               $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'];
                    $data['product_image'] = $product_image;
                 } 
                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 ();
         }
}

【讨论】:

    【解决方案2】:

    您的 categoryID 与类别不匹配

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      相关资源
      最近更新 更多