Stavo testando chiavi esterne in MySQL. L'obiettivo è impedire l'immissione diin table2
, che non esiste in table1
. Speravo che l'utilizzo di foreign keys
comportasse semplicemente il mancato inserimento di una riga, ma sembra che questo enorme errore sia stato generato. Come gestisco questo in PHP? C'è un modo per rendere MySQL semplicemente non inserire la riga e non restituire alcuna riga restituita?Come gestire l'errore di chiave esterna di MySQL in PHP?
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