codeigniter php上的数据库错误
无法添加或更新子行:外键约束失败(codeigniter php上的数据库错误,php,mysql,codeigniter,Php,Mysql,Codeigniter,无法添加或更新子行:外键约束失败(warehouse\u inventory\u systeminventory\u products,约束fk\u category外键(category\u Id)引用category) 在库存产品(产品名称,类别Id,品牌名称,单价,录入日期,数量)中插入值(0,0,'1',120',2016-03-31',12') 运行的代码是 <?php $query = $this->db->query('SELECT * FROM categor
warehouse\u inventory\u system
inventory\u products
,约束fk\u category
外键(category\u Id
)引用category
)
在库存产品
(产品名称
,类别Id
,品牌名称
,单价
,录入日期
,数量
)中插入值(0,0,'1',120',2016-03-31',12')
运行的代码是
<?php
$query = $this->db->query('SELECT * FROM category');
foreach ($query->result() as $row) {
echo '<option value="'. $row->Category_Id. '">'. $row->Category_Name. '</option>';
}
?>
Insert code is:
function create(){
$this->form_validation->set_rules('Unit_Price','Unit_Price','trim|xss_clean|numeric|is_natural_no_zero|min_length[2]');
$this->form_validation->set_rules('Quantity','Quantity','trim|xss_clean|numeric|is_natural_no_zero');
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$data = array(
'Product_Name' => $this->input->post('Product_Name'),
'Category_Id' => $this->input->post('Category_Id'),
'Brand_Name' => $this->input->post('Brand_Name'),
'Unit_Price' => $this->input->post('Unit_Price'),
'Date_of_Entry' => $this->input->post('Date_of_Entry'),
'Quantity' => $this->input->post('Quantity')
);
$this->Admin_model->add_items($data);
$this->index();
}
}
Admin_model
function add_items($data){
$this->db->insert('inventory_products', $data);
return;
}
插入代码为:
函数create(){
$this->form_validation->set_rules('Unit_Price','Unit_Price','trim | xss_clean | numeric | is_natural_no_zero | min_length[2]);
$this->form_validation->set_rules('Quantity'、'Quantity'、'trim | xss|u clean | numeric | is|u natural_no_zero');
如果($this->form\u validation->run()==FALSE)
{
$this->index();
}
其他的
{
$data=数组(
“产品名称”=>$this->input->post('Product\u Name'),
'Category\u Id'=>this->input->post('Category\u Id'),
'Brand_Name'=>this->input->post('Brand_Name'),
“单价”=>this->input->post(“单价”),
“输入日期”=>$this->input->post('Date\u of\u Entry'),
“数量”=>$this->input->post('Quantity')
);
$this->Admin\u model->add\u items($data);
$this->index();
}
}
管理模式
函数添加项($data){
$this->db->insert('inventory\u products',$data);
返回;
}
您正在尝试向目录产品表插入一条新记录,该表的目录产品id
等于0,并且由于目录产品引用了目录表,并且在目录表中没有目录id=0的记录,因此抛出外键约束失败错误
检查您的
和
并确保它们的名称
属性与您在PHP代码中读取的属性匹配。例如:必须有一个名为Category\u Id
的标记,尝试将'Category\u Id'=>$this->input->post('Category\u Id')
更改为定义的值,而不是$\u post
值,然后查看错误是否仍然存在。如果没有发生错误,可以追溯到$this->input->post('Category_Id')
未正确设置(或在本例中设置为0
),导致SQL报告未提供此所需键。您在该表上有外键引用,但没有插入值。因此,它抛出的错误0
不是一个有效的值。感谢您的深入了解