执行update更新时,出错,求大神指点
程序员文章站
2022-06-11 15:28:28
...
public function save(){
if(IS_POST){
$post = I('post.');
if(!isset($post['pkid']) || empty($post['pkid'])){
$result = $this->CourseModel->addInfo($post);
}else{
$result = $this->CourseModel->updateInfo($post);
}
if ($result!==false) {
$this->redirect("Course/index",'',1, '保存成功');
} else {
$this->redirect("Course/index",'',1, '保存失败');
}
}
}
public function updateInfo($data=array()){
if(empty($data)){
return false;
}
$data['modify_time'] = dateTime();
return $this->data($data)->save();
}
ERR: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`elearning`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`pkid`) ON DELETE CASCADE ON UPDATE CASCADE)
怎么解决那?
if(IS_POST){
$post = I('post.');
if(!isset($post['pkid']) || empty($post['pkid'])){
$result = $this->CourseModel->addInfo($post);
}else{
$result = $this->CourseModel->updateInfo($post);
}
if ($result!==false) {
$this->redirect("Course/index",'',1, '保存成功');
} else {
$this->redirect("Course/index",'',1, '保存失败');
}
}
}
public function updateInfo($data=array()){
if(empty($data)){
return false;
}
$data['modify_time'] = dateTime();
return $this->data($data)->save();
}
ERR: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`elearning`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`pkid`) ON DELETE CASCADE ON UPDATE CASCADE)
回复讨论(解决方案)
被修改的表存在外键约束,你修改了不存在的外键约束的记录
被修改的表存在外键约束,你修改了不存在的外键约束的记录
怎么解决那?
你要么取消外键,要么在修改前先确认键存在