我試圖在數據庫中插入2行,第二行需要第一行的自動遞增ID。 我已經與LAST_INSERT_ID()
測試,但我有一個MysqlIntegrityConstraintViolationException
:如何使用MySQL獲取最後一個ID插入
Cannot add or update a child row : a foreign key constraint fails ('customer_user_role', CONSTRAINT 'fk_customer_user_role_customer_id' FOREIGN KEY ('customer_user_role_id') REFERENCES 'customer' ('customer_id') ON DELETE NO ACTION ON UPDATE NO ACTION)
我的SQL請求是:
sqlu"INSERT INTO customer (name, email, phone, isActivated) VALUES ($name, $email, $phoneNumber, $isActivated)"
sqlu"INSERT INTO customer_user_role (customer_id, user_id, customer_roletype_id) VALUES (LAST_INSERT_ID(), $userId, $roleTypeId)"
(我的工作寬度播放&油滑所以這爲什麼特定語法)
當我在PhpMyAdmin上執行此操作時,會顯示相同的錯誤,我不知道如何繼續...
我已經看過這個文檔,它使我錯誤,用'last_insert_id()'和'mysql_insert_id()' – Azuken
你調試過嗎?首先調試看看你是否真的獲得了價值。您的列是否設置爲自動增量? – johnny
當我從PHPMyAdmin執行時,它返回我0 ...插入課程後。 – Azuken