2016-09-01 35 views
-1

(!) 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 (mowanj1_in605 . sportEvent , CONSTRAINT sportEvent_ibfk_1 FOREIGN KEY (eventID) REFERENCES event (eventID))' in /home/mowanj1/public_html/Web2/Assignment1/createAthlete.html.php on line 222錯誤的(PK的和FK在正確的順序嘗試)

(!) PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mowanj1_in605 . sportEvent , CONSTRAINT sportEvent_ibfk_1 FOREIGN KEY (eventID) REFERENCES event (eventID)) in /home/mowanj1/public_html/Web2/Assignment1/createAthlete.html.php on line 222

$createQuery = "CREATE TABLE sportEventMedal 
sportEventMedalID INT NOT NULL AUTO_INCREMENT, sportEventID INT, medalID INT, 
PRIMARY KEY (sportEventMedalID), FOREIGN KEY (sportEventID) REFERENCES sportEvent(sportEventID), FOREIGN KEY (medalID) REFERENCES medal(medalID) 
$pdo->exec($createQuery); 
while (! feof($file)) { 
    $temp = fgetcsv($file); 
    $insertQuery = "INSERT INTO sportEvent(eventID, sportID) VALUES('$temp[0]','$temp[1]')"; 
    $pdo->exec($insertQuery); 
} 
fclose($file); 
+0

$的createQuery =「CREATE TABLE sportEventMedal – Nick

+0

sportEventMedalID INT NOT NULL AUTO_INCREMENT, sportEventID INT, medalID INT, – Nick

+0

PRIMARY KEY(sportEventMedalID), 外鍵(sportEventID)參考文獻sportEvent(sportEventID), 外鍵(medalID)參考獎牌(medalID) – Nick

回答

0

加載sportEvent之前,您應該加載eventsport。如果您已經正確執行此操作,那麼您的csv文件包含不一致的數據 - 包含無效eventID值的行。一種選擇是忽略這些行,您可以在嘗試插入之前查詢事件是否存在,或者您可以忽略該錯誤。另一種選擇是爲缺少的ID創建一個事件,然後重試插入到sportEvent。最簡單和最差的選擇是刪除外鍵約束,這將允許您將無效數據插入到數據庫中,並且現在忽略該問題。