【发布时间】:2015-09-08 23:19:16
【问题描述】:
我正在使用 MySQLi 连接到我的数据库,我的问题是我想在数据库中插入,但我收到此错误:
错误:无法添加或更新子行:外键约束 失败(
edi_tracker_revised.edi_transaction,约束edi_transaction_ibfk_1外键 (edi_doc_type_id) 参考edi_doc_type(edi_doc_type_id) 更新时删除级联 级联)
这是我的插入代码。
if ($doc_type == '812') {
$edi_doc_type_id = "SELECT edi_doc_type_id FROM `edi_doc_type` WHERE doc_type = 812 " ;
$sql = "INSERT INTO edi_transaction (`sender_id`,`receiver_id`,`isa_number`,`transaction_date`,`gs_number`,`edi_doc_type_id`,`ref_number`,`file_name`,`file_path`,`creation_date`)
VALUES ('$sender_id','$receiver_id','$isa_number','$transaction_date','$gs_number','$edi_doc_type_id','$ref_number','$file_name','$file_path',NOW()) " ;
$ref_numberArray = explode(',' ,$ref_number); // the ref_number contains the invoice numbers
//Checking query if it is successful
if ($conn->query($sql) === false){
trigger_error('Wrong SQL: ' . $sql . 'Error: ' . $conn->error , E_USER_ERROR);
}else {
echo "Successful ! Data is inserted in database ^__^" ;
}
}
当我说手动时,我使用 phpmyadmin 插入,现在如果我在 edi_transaction 之前先手动填充我的 edi_doc_type,我该如何解决?我需要 edi_transaction 表中的 edi_doc_type_id。有人可以帮帮我吗?
【问题讨论】:
-
外键约束的重点是不要在受键约束的表中插入内容。根据您的表之间的关系,您永远不必做您所做的事情。
-
但我认为这是一种方式,所以我的数据不会有冗余,我认为如果我使用代码插入,我会有冗余数据的趋势......那又如何我应该怎么做?你能帮我吗?