[英]Codeigniter|: error inserting image into mysql
每當我將圖像插入數據庫時,都會出現此錯誤
錯誤號: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);
}
如果上傳了圖片,您的代碼將進行圖片插入,如果表單經過驗證,則將進行整個數據插入。
您的產品表具有categorie_id
字段約束,不能為空,並且應存在於categorie表上,因此出現了以上錯誤。
您應該通過添加條件(如果已上傳)添加有條件的圖片數據,將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 ();
}
}
您的categoryID與類別不匹配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.