我在MySQL中測試外鍵。目標是防止id
輸入table2
,這在table1
中不存在。我希望使用foreign keys
只會導致一行沒有被插入,但它似乎拋出了這個巨大的錯誤。我如何在PHP中處理這個問題?有沒有辦法讓MySQL不插入行並返回沒有行返回?如何處理Php中的MySQL外鍵錯誤?
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
wp-db
.borrowed
, CONSTRAINTborrowed_ibfk_1
FOREIGN KEY (employeeid
) REFERENCESemployee
(id
) ON DELETE CASCADE ON UPDATE CASCADE)' in C:\web\apache\htdocs\dev\foreign.php:10Stack trace:
#0 C:\web\apache\htdocs\dev\foreign.php(10): PDOStatement->execute()
#1 {main} thrown in C:\web\apache\htdocs\dev\foreign.php on line 10