不能添加或更新子行:外键约束失败。
你的PHP应用在使用PDO连接MySQL时遇到了外键约束错误:
'pdo_code' => '23000',
'db_code' => 1216,
'db_error' => 'Cannot add or update a child row: a foreign key constraint fails'
try {
$pdo->beginTransaction();
$parentStmt = $pdo->prepare("INSERT INTO parent_table (name) VALUES (?)");
$parentStmt->execute(['parent_value']);
$parentId = $pdo->lastInsertId();
$childStmt = $pdo->prepare("INSERT INTO child_table (parent_id, value) VALUES (?, ?)");
$childStmt->execute([$parentId, 'child_value']);
$pdo->commit();
echo "数据插入成功";
} catch (PDOException $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
if ($e->getCode() == '23000' || $e->getCode() == '1216') {
echo "外键约束错误: " . $e->getMessage();
} else {
throw $e;
}
}