Php (SQL)包含空外键错误的更新行
我正在做一个数据库项目。当我尝试更新包含空外键的行时,它给了我Php (SQL)包含空外键错误的更新行,php,mysql,Php,Mysql,我正在做一个数据库项目。当我尝试更新包含空外键的行时,它给了我无法添加或更新子行:外键约束失败(“Archelogy”。“artifact”,约束“location”外键(“location\u id”)引用“location”(“location\u id”)ON DELETE NO ACTION ON UPDATE SET NULL错误。我可以添加包含NULL外键的行,但无法更新它 这是我的php代码: function editArtifact($editData){ $query = "
无法添加或更新子行:外键约束失败(“Archelogy”。“artifact”,约束“location”外键(“location\u id”)引用“location”(“location\u id”)ON DELETE NO ACTION ON UPDATE SET NULL
错误。我可以添加包含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
数据库设计:
我不明白,我可以插入一个空列,但不能更新它。我认为您正在尝试更新
工件一个位置id
值,该值在位置
表中不存在。同时检查您的post参数位置id=null
。在表中可能没有一个位置id
为空位置
表。希望这有帮助
您的工件表中的位置\u id
应允许NULL
值
注意:要使用SET NULL规则进行更新/删除操作,外键列应允许NULL值,否则SET NULL规范将因生成错误消息而失败。我认为您正在尝试更新工件
表中不存在的位置id
值。同时检查您的post参数location\u id=null
。您的location
表中可能没有一个location\u id
为null。希望这对您有所帮助
您的工件表中的位置\u id
应允许NULL
值
注意:要使用SET NULL规则进行更新/删除操作,外键列应允许NULL值,否则SET NULL规范将因生成错误消息而失败。好的,伙计们,我解决了这个问题。问题出在我的php代码上。我正试图用isset构造确定NULL值。当我试图确定NULL时,w第i个“=”,问题已修复
固定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应用程序的数据。我不知道它为什么发布空字符串。好的,伙计们,我解决了这个问题。问题出在我的php代码上。我试图用isset构造确定空值。当我尝试用“==”确定空值时,问题已经解决
固定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应用程序的数据。我不知道为什么它会发布空字符串。位置
表是您的主表。正确吗?如果是,则位置表位置id
和工件位置id
在更新时应该是相同的类型。@elumalai\u kp位置表有阳离子id作为主键,工件表也有location id作为location表引用的外键。location id的类型为int[11]。您可以显示位置和工件表的表结构吗?如果不查看表结构,就无法说出添加数据库设计的确切原因。我希望这将帮助您解决此问题。位置
表是您的主表。正确吗?如果是,位置表位置id
和工件位置id更新时,代码>的类型应相同。@elumalai_kp位置表的位置id为主键,工件表的位置id为位置表引用的外键。位置id的类型为int[11]。您可以显示位置和工件表的表结构吗?如果不查看表结构,就无法说出添加数据库设计的确切原因。我希望这能帮助您解决此问题。位置表的主键是位置id,因此我不能向此表添加空位置id。我只想将行更新为工件表which包含null location_id作为外键。location表的主键是location_id,因此我无法将null location_id添加到此表。我只想将行更新到工件表,该工件表包含null location_id作为外键。