如何解决错误号:1452-无法添加或更新子行:外键约束失败

时间:2019-02-09 19:03:54

标签: php codeigniter phpmyadmin

  

错误号:1452

     

无法添加或更新子行:外键约束失败(smpstudent,CONSTRAINT FK_ParentStudent外键(p_id)参考parentp_id)在删除CASCADE时在更新CASCADE上)

     

插入students_idstudent_codef_namel_namedobgender,{{1 }},addresstelusername)值(``,'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_ids_id),  键s_idp_id),  约束p_id外键(FK_ParentStudent)参考p_idparent)在删除级联上在更新级联上 )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_idp_id) )ENGINE = InnoDB AUTO_INCREMENT = 10 DEFAULT CHARSET = latin1

2 个答案:

答案 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限制,您可能对此已经限制了。