我一直在閱讀一些問題同樣的問題我有,但還沒有找到解決辦法MySQL的錯誤:當插入和更新:無法添加或更新子行,外鍵約束失敗
數據庫Estructure
CREATE TABLE material (
cve_mat varchar(12),
type_mat varchar(20),
author_mat varchar(40),
status varchar(2),
primary key(cve_mat)
) ENGINE = INNODB;
CREATE TABLE users (
cve_user varchar(8),
name_user varchar(25),
lastn_user varchar(25),
email_user varchar(25),
primary key(cve_user)
)ENGINE = INNODB;
CREATE TABLE material_user (
cve_mat varchar(8),
cve_user varchar(8),
start_mat date,
end_mat date,
foreign key(cve_mat) references material(cve_mat)
ON UPDATE CASCADE
ON DELETE CASCADE,
foreign key(cve_user) references users(cve_user)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE = INNODB;
當執行這些指令:
......
$query = "INSERT INTO material_user VALUES ('$cvemat','$ncontrol',NOW(),DATE_ADD(current_date, interval '$days' day))";
$result = mysql_query($query) or die(mysql_error());
if ($result) {
$query = "UPDATE material SET status = 'YES' WHERE material.cve_mat = '$cvemat'";
$result = mysql_query($query) or die(mysql_error());
if ($result){
return true;
} else {
return false;
}
} else {
return false;
}
並顯示此錯誤:
Cannot add or update a child row: a foreign key constraint fails (`biblioteca/material_prestamo`, CONSTRAINT `material_prestamo_ibfk_1` FOREIGN KEY (`cve_mat`) REFERENCES `material` (`cve_mat`) ON DELETE CASCADE ON UPDATE CASCADE)
我將非常感謝您的幫助!
有兩種說法,在裏面做你看到提到的錯誤? –
根據錯誤,是爲語句插入! – SoldierCorp
您是否確定''cvemat'存在於'material'表中?此外,數據類型的長度不material_user.cve_mat'之間的匹配'和'material.cve_mat' –