2017-10-06 137 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的工件表。 –