0

Everytime I insert an image to database i got this error

Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (herbalcebu.product, CONSTRAINT product_ibfk_1 FOREIGN KEY (categorie_id) REFERENCES categorie (categorie_id))

INSERT INTO product (product_image) VALUES ('q3.jpg')

Filename: C:/xampp/htdocs/HerbalCebu/system/database/DB_driver.php

Line Number: 691

This is my Controller Code

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

My Model Code

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

    }
2

2 Answers 2

1

Your code is doing an image insertion if image is uploaded, then do a whole data insert if form is validated.

Your product table have a categorie_id field constraint, that cannot be empty & should be exist on the categorie table, so you got the error above.

You should merge the product_image data with the whole product data by adding conditional if uploaded add additional uploaded image data :

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 ();
         }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your categoryID does not match a category

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.