2017-10-06 134 views
0

我正在处理数据库项目。当我尝试更新包含空外键的行时,它会给我Cannot add or update a child row: a foreign key constraint fails ("archaelogy"."artifact", CONSTRAINT "location" FOREIGN KEY ("location_id") REFERENCES "location" ("location_id") ON DELETE NO ACTION ON UPDATE SET NULL错误。我可以添加一个包含空外键但不能更新的行。(SQL)更新包含的空行外键错误

这是我的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

我不明白,我可以插入一个空列,但无法更新它。

+0

'location'表是您的主表。正确?如果在更新时,位置表'location_id'和工件'location_id'应该是相同的类型。 –

+0

@elumalai_kp位置表的location_id作为主键,工件表的location_id也与位置表引用的外键一样。 location_ids的类型是int [11]。 –

+0

您可以显示位置和工件表的表格结构吗? –

回答

1

好吧,我解决了这个问题。问题出在我的php代码上。我试图用isset构造来确定空值。当我尝试用'==='确定null时,问题已修复。

修正PHP代码:

<?php 
require_once "DbOperations.php"; 
$response=array(); 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
    if (isset($_POST['artifact_id'])) { 

$editData; 
if($_POST['artifact_id']!=='null'){ 
    $editData['artifact_id']=$_POST['artifact_id']; 
    if($_POST['worker_id']!=='null'){ 
    $editData['worker_id']=$_POST['worker_id']; 
    }else{ 
    $editData['worker_id']=null; 
    } 
    if($_POST['image']!=='null'){ 
    $editData['image']=$_POST['image']; 
    }else{ 
    $editData['image']=null; 
    } 
    if($_POST['period']!=='null'){ 
    $editData['period']=$_POST['period']; 
    }else{ 
    $editData['period']=null; 
    } 
    if($_POST['location_id']!=='null'){ 
    $editData['location_id']=$_POST['location_id']; 
    }else{ 
    $editData['location_id']=null; 
    } 
    if($_POST['coordinate_x']!=='null'){ 
    $editData['coordinate_x']=$_POST['coordinate_x']; 
    }else{ 
    $editData['coordinate_x']=null; 
    } 
    if($_POST['coordinate_y']!=='null'){ 
    $editData['coordinate_y']=$_POST['coordinate_y']; 
    }else{ 
    $editData['coordinate_y']=null; 
    } 
    if($_POST['type']!=='null'){ 
    $editData['type']=$_POST['type']; 
    }else{ 
    $editData['type']=null; 
    } 
    if($_POST['comment']!=='null'){ 
    $editData['comment']=$_POST['comment']; 
    }else{ 
    $editData['comment']=null; 
    } 

    $db = new DbOperations(); 
    $db->editArtifact($editData); 
    } 
    }else{ 
     $response['error']=true; 
     json_encode($response); 
    } 
    } 
?> 

感谢大家的努力来解决问题。我从一个Android应用发布数据。我不知道它为什么发布空字符串。

2

我认为您正在尝试更新artifact a location_id值,该值在location表中不存在。另请检查您的发布参数location_id=null。您的location表中可能没有location_id为空。希望这可以帮助!

您的location_idartifact表中应该允许NULL的值。

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

+0

位置表的主键是location_id,因此我无法将空位置标识添加到此表中。我只想将行更新为包含null location_id作为foreign_key的工件表。 –