错误号:1452
无法添加或更新子行:外键约束失败(
smp
。student
,CONSTRAINTFK_ParentStudent
外键(p_id
)参考parent
(p_id
)在删除CASCADE时在更新CASCADE上)插入
student
(s_id
,student_code
,f_name
,l_name
,dob
,gender
,{{1 }},address
,tel
,username
)值(``,'cbg','sdfsdf','sdfsd','02 / 13/2019','male', 'fgfhnf','','admin','pAQ7oL + r8QJNgy2siN5moyJUhrgSj5tq3Ai5U2ngMyvaQZDsS9ooPbfcCm8qKEWi1C6nbYdMCyscz6ngU + 1Tiw ==')文件名:C:/xampp/htdocs/SMP/system/database/DB_driver.php
行号:691
我收到此错误,我认为这与控制器中的功能有关
password
模型
public function register_students() {
// $this->load->model('Register_model','multi_model',TRUE);
$encrypted_password1 = $this->encrypt->encode($this->input->post('p_pwd'));
$parent_data = array(
'p_id' => '',
'parent_code' => $this->input->post('parent_code'),
'f_name' => $this->input->post('p_first_name'),
'l_name' => $this->input->post('p_last_name'),
'dob' => $this->input->post('p_dob'),
'address' => $this->input->post('p_address'),
'tel' => $this->input->post('p_tel_no'),
'email' => $this->input->post('email'),
'username' => $this->input->post('p_username'),
'password' => $encrypted_password1,
);
// var_dump($student_data);
// var_dump($parent_data);
$result = $this->Model_Action->insertTable('parent', $parent_data);
$encrypted_password = $this->encrypt->encode($this->input->post('pwd'));
$student_data = array(
's_id' => '',
'student_code' => $this->input->post('student_code'),
'f_name' => $this->input->post('first_name'),
'l_name' => $this->input->post('last_name'),
'dob' => $this->input->post('dob'),
'gender' => $this->input->post('gender'),
'address' => $this->input->post('address'),
'tel' => $this->input->post('tel_no'),
'username' => $this->input->post('username'),
'password' => $encrypted_password,
);
$result = $this->Model_Action->insertTable('student', $student_data);
// $result = $this->multi_model->student_register($student_data, $parent_data);
if($result) {
redirect('student');
}
else
{
redirect('student');
}
}
创建表 function insertTable($table, $data) {
$this->db->insert($table, $data);
return $this->db->insert_id();
}
(
student
int(11)NOT NULL AUTO_INCREMENT,
s_id
varchar(50)NOT NULL,
student_code
varchar(150)NOT NULL,
f_name
varchar(250)NOT NULL,
l_name
文字NOT NULL,
DOB
枚举('male','female')NOT NULL,
gender
varchar(450)非空,
address
int(50)NOT NULL,
tel
varchar(100)NOT NULL,
username
varchar(150)NOT NULL,
password
int(11)NOT NULL,
主键(p_id
),
键s_id
(s_id
),
键s_id
(p_id
),
约束p_id
外键(FK_ParentStudent
)参考p_id
(parent
)在删除级联上在更新级联上
)ENGINE = InnoDB AUTO_INCREMENT = 16 DEFAULT CHARSET = latin1
创建表p_id
(
parent
int(11)NOT NULL AUTO_INCREMENT,
p_id
varchar(100)NOT NULL,
parent_code
varchar(150)NOT NULL,
f_name
varchar(250)NOT NULL,
l_name
文字NOT NULL,
DOB
varchar(250)NOT NULL,
address
varchar(50)NOT NULL,
tel
varchar(250)NOT NULL,
email
varchar(250)NOT NULL,
username
varchar(250)NOT NULL,
主键(password
),
键p_id
(p_id
)
)ENGINE = InnoDB AUTO_INCREMENT = 10 DEFAULT CHARSET = latin1
答案 0 :(得分:1)
分析PHP代码,我认为您首先插入了父代,而用于插入的函数会返回一个父代ID,但您并未在学生插入页面上使用它。如果将其添加到学生数组中,它将解决您的问题:
public function register_students() {
{...}
$p_id = $this->Model_Action->insertTable('parent', $parent_data); // Parent it returned
$encrypted_password = $this->encrypt->encode($this->input->post('pwd'));
$student_data = array(
's_id' => '',
'p_id' => $p_id, // Adding parent id to student_data
{...}
);
$result = $this->Model_Action->insertTable('student', $student_data);
{...}
}
有关SQL错误的说明
您会收到此SQL异常错误,因为在student
表上定义了一个不为空外键p_id
引用了父表上的p_id
:< / p>
CREATE TABLE student (
s_id int(11) NOT NULL AUTO_INCREMENT,
student_code varchar(50) NOT NULL,
f_name varchar(150) NOT NULL,
l_name varchar(250) NOT NULL,
DOB text NOT NULL, gender enum('male','female') NOT NULL,
address varchar(450) NOT NULL,
tel int(50) NOT NULL,
username varchar(100) NOT NULL,
password varchar(150) NOT NULL,
p_id int(11) NOT NULL,
PRIMARY KEY (s_id),
KEY s_id (s_id),
KEY p_id (p_id),
CONSTRAINT FK_ParentStudent FOREIGN KEY (p_id) REFERENCES parent (p_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1
请注意,p_id int(11) NOT NULL
使您无法传递任何内容,而不能传递有效的ID引用到parent
表。也就是说,您不能传递''
或null
或父表中有效ID以外的任何其他值。如果您希望此外键具有默认值,请编辑SQL以在p_id
表上将默认值包括到student
:
p_id int(11) DEFAULT NULL
答案 1 :(得分:0)
如果p_id
是您的主键,则不应在插入时通过它。从'p_id' => '',
$parent_data
如果无法删除或更新数据。请检查数据库表上的foreign_key限制,您可能对此已经限制了。