Php Codeigniter |:将图像插入mysql时出错
每次我将图像插入数据库时,都会出现此错误 错误号码:1452 无法添加或更新子行:外键约束失败 (Php Codeigniter |:将图像插入mysql时出错,php,codeigniter,Php,Codeigniter,每次我将图像插入数据库时,都会出现此错误 错误号码:1452 无法添加或更新子行:外键约束失败 (herbalcebuproduct,约束product\u ibfk\u 1外键 (categorie\u id)引用categorie(categorie\u id) 将值('q3.jpg')插入product(product\u image)中 文件名:C:/xampp/htdocs/HerbalCebu/system/database/DB_driver.php 电话号码:691 这是我的控制
herbalcebu
product
,约束product\u ibfk\u 1
外键
(categorie\u id
)引用categorie
(categorie\u id
)
将值('q3.jpg')插入product
(product\u image
)中
文件名: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);
}
您的categoryID与类别不匹配如果上载了图像,则代码正在执行图像插入,如果表单已验证,则执行整个数据插入 您的产品表有一个
categorie\u id
字段约束,该约束不能为空&应该存在于categorie表上,因此出现了上述错误
您应该通过添加条件(如果已上载)将产品\u图像
数据与整个产品数据合并添加其他已上载图像数据:
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 ();
}
}
检查这里:可能重复的