gpt4 book ai didi

php - (SQL) 更新包含空外键错误的行

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:07 24 4
gpt4 key购买 nike

我正在从事一个数据库项目。当我尝试更新包含空外键的行时,它给我 无法添加或更新子行:外键约束失败(“archaeology”。“artifact”,CONSTRAINT“location” FOREIGN KEY(“location_id” ) REFERENCES "location"("location_id") ON DELETE NO ACTION ON UPDATE SET NULL 错误。我可以添加包含空外键但无法更新它的行。

这是我的 php 代码:

function editArtifact($editData){
$query = "UPDATE artifact SET worker_id=?, image=?, period=?,location_id=?, coordinate_x=?, coordinate_y=?, type=?, comment=?) WHERE artifact_id=?";
$stmt = $this->con->prepare($query);
$stmt->bind_param("ibsiddsss", $editData['worker_id'],$editData['image'],$editData['period'],$editData['location_id'],$editData['coordinate_x'],$editData['coordinate_y'],$editData['type'],$editData['comment'],$editData['artifact_id']);

if ($stmt->execute()) {
echo json_encode(array("editStatus"=>array('success'=>true)));
}else{
echo json_encode(array("editStatus"=>array('success'=>false)));
}

$stmt->close();

这是我的帖子:

artifact_id=qw & worker_id=1 & image=null & period=null & location_id=null & coordinate_x=null & coordinate_y=null & type=null & comment=null

数据库设计:

database design

我不明白,我可以插入空列但不能更新它。

最佳答案

我认为您正在尝试将 artifact 更新为 location_id 值,该值在 location 表中不存在。还要检查您的帖子参数 location_id=null。您的 location 表中可能没有作为 null 的 location_id。希望这可以帮助!

artifact 表中的location_id 应该允许NULL 值。

注意:要将 SET NULL 规则用于更新/删除操作,外键列应允许 NULL 值,否则 SET NULL 规范将因生成错误消息而失败。

关于php - (SQL) 更新包含空外键错误的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46609809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com