Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 错误';s(按正确顺序尝试了PK和FK)_Php_Sql_Webpage - Fatal编程技术网

Php 错误';s(按正确顺序尝试了PK和FK)

Php 错误';s(按正确顺序尝试了PK和FK),php,sql,webpage,Php,Sql,Webpage,(!)致命错误:未捕获异常“PDOException”并显示消息“SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(mowanj1\u in605sportEvent,constraintsportEvent\u ibfk\u 1外键(eventID)引用event(eventID)”在/home/mowanj1/public_html/Web2/Assignment1/createatherine.html.php的第222行 (!)PDOExcepti

(!)致命错误:未捕获异常“PDOException”并显示消息“SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
mowanj1\u in605
sportEvent
,constraint
sportEvent\u ibfk\u 1
外键(
eventID
)引用
event
eventID
)”在/home/mowanj1/public_html/Web2/Assignment1/createatherine.html.php的第222行

(!)PDOException:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
mowanj1_in605
sportEvent
,约束
sportEvent\u ibfk_1
外键(
eventID
)引用
event
eventID
)在第222行的/home/mowanj1/public_html/Web2/Assignment1/createatherine.html.php中


在加载
sportEvent
之前,您应该加载
event
sport
。如果您已经正确加载了
sportEvent
。则您的csv文件包含不一致的数据-包含无效
eventID
值的行。一个选项是忽略这些行,您可以在尝试插入或删除之前查询事件是否存在您可以忽略此错误。另一个选项是为缺少的ID创建事件,然后重试插入
sportEvent
。最简单也是最糟糕的选项是删除外键约束,这将允许您将无效数据插入数据库并暂时忽略此问题。

$createQuery=“创建表SportEventMedalPorteventMedalid INT NOT NULL自动递增,sportEventID INT,medalID INT,主键(sportEventMedalID),外键(sportEventID)引用sportEvent(sportEventID),外键(medalID)引用medalID(medalID)$pdo->exec($createQuery);而(!feof($file)){$temp=fgetcsv($file);$insertQuery=“插入到sportEvent(eventID,sportID)值(“$temp[0]”,“$temp[1]”);$pdo->exec($insertQuery);}fclose($file);
$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);