I got an error like this : Cannot add or update a child row: a foreign key constraint fails when insert my data to database phpmyadmin using Codeigniter.
This is my error message :
Cannot add or update a child row: a foreign key constraint fails (
tugasakhir
.tb_pasien
, CONSTRAINTtb_pasien_ibfk_1
FOREIGN KEY (kode_pasien
) REFERENCEStb_jk
(kode_jk
) ON DELETE CASCADE ON UPDATE CASCADE)INSERT INTO
tb_pasien
(nama_pasien
,email_pasien
,username
,password
,alamat_pasien
,tanggal_lahir
,umur
,kode_jk
,no_telp
,no_antrian
) VALUES ('nandoku', 'gustiayuri@gmail.com', 'nandoku', '827ccb0eea8a706c4c34a16891f84e7b', 'dalung', '11/12/1995', '21', 'L', '567890', 0)
This is my controller :
public function insert()
{
$this->load->database();
$this->load->model('p_model');
$data_pasien = array(
"nama_pasien" => $this->input->post('nama_pasien'),
"email_pasien" => $this->input->post('email_pasien'),
"username" => $this->input->post('username'),
"password" => md5($this->input->post('password')),
"alamat_pasien" => $this->input->post('alamat_pasien'),
"tanggal_lahir" => $this->input->post('tanggal_lahir'),
"umur" => $this->input->post('umur'),
"kode_jk" => $this->input->post('kode_jk'),
"no_telp" => $this->input->post('no_telp'),
"no_antrian" => $this->input->post('no_antrian')
);
$data_jk = array(
'nama_jk' => $this->input->post('nama_jk')
);
$this->p_model->insert_entry($data_pasien, $data_jk);
}
Model :
function insert_entry($data_pasien, $data_jk) {
$this->db->insert('tb_pasien', $data_pasien);
$data_pasien['kode_jk'] = $this->db->insert_id();
$this->db->insert('nama_jk', $data_pasien);
}
View
<!-- form start -->
<form role="form" action="<?php echo base_url().'index.php/a_controller/tambahmanual' ?>" method="post">
<div class="box-body">
<div class="form-group">
<label for="exampleInputNama">Nama Pasien</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" name="nama_pasien" id="exampleInputNama" placeholder="Nama Pasien">
</div>
</div>
<div class="form-group">
<label for="exampleInputEmail">Email Pasien</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input type="text" class="form-control" name="email_pasien" id="exampleInputEmail" placeholder="Email Pasien">
</div>
</div>
<div class="form-group">
<label for="exampleInputUsername">Username</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" name="username" id="exampleInputUsername" placeholder="Username">
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword">Password</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-key"></i></span>
<input type="text" class="form-control" name="password" id="exampleInputPassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="exampleInputAlamat">Alamat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-home"></i></span>
<input type="text" class="form-control" name="alamat_pasien" id="exampleInputAlamat" placeholder="Alamat">
</div>
</div>
<div class="form-group">
<label>Tanggal lahir</label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" name="tanggal_lahir" class="form-control">
</div>
<!-- /.input group -->
</div>
<div class="form-group">
<label for="exampleInputUmur">Umur</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" name="umur" id="exampleInputUmur" placeholder="Umur">
</div>
</div>
<div class="form-group">
<label>Jenis Kelamin</label>
<select name="kode_jk" class="form-control">
<option value="1">L</option>
<option value="2">P</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputTelp">No. Telp</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone"></i></span>
<input type="text" class="form-control" name="no_telp" id="exampleInputTelp" placeholder="No. Telp">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
This is my tables :
CREATE TABLE `tb_pasien` ( `kode_pasien` int(4) NOT NULL AUTO_INCREMENT, `nama_pasien` varchar(20) NOT NULL, `email_pasien` varchar(20) NOT NULL, `username` varchar(20) NOT NULL, `password` varchar(255) NOT NULL, `alamat_pasien` text NOT NULL, `tanggal_lahir` date NOT NULL, `umur` int(2) NOT NULL, `kode_jk` int(2) NOT NULL, `no_telp` int(20) NOT NULL, `no_antrian` int(3) NOT NULL, PRIMARY KEY (`kode_pasien`), CONSTRAINT `tb_pasien_ibfk_1` FOREIGN KEY (`kode_pasien`) REFERENCES `tb_jk` (`kode_jk`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 CREATE TABLE `tb_jk` ( `kode_jk` int(2) NOT NULL AUTO_INCREMENT, `nama_jk` varchar(1) NOT NULL, PRIMARY KEY (`kode_jk`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
Note : kode_jk is foreign key on tb_pasien table.
What should i do for this error message?
Note : The number of columns in the child table and parent table specified in the FOREIGN KEY and REFERENCES must be the same and also the data type.
Table 'tb_pasien' :
CREATE TABLE `tb_pasien` (
`kode_pasien` int(11) NOT NULL AUTO_INCREMENT,
`nama_pasien` varchar(20) NOT NULL,
`email_pasien` varchar(20) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(255) NOT NULL,
`alamat_pasien` text NOT NULL,
`tanggal_lahir` date NOT NULL,
`umur` int(2) NOT NULL,
`kode_jk` int(11) NOT NULL,
`no_telp` int(20) NOT NULL,
`no_antrian` int(3) NOT NULL,
PRIMARY KEY (`kode_pasien`),
CONSTRAINT `tb_pasien_ibfk_1` FOREIGN KEY (`kode_jk`) REFERENCES `tb_jk` (`kode_jk`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
CREATE TABLE `tb_jk` (
`kode_jk` int(11) NOT NULL AUTO_INCREMENT,
`nama_jk` varchar(1) NOT NULL,
PRIMARY KEY (`kode_jk`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
在插入 tb_pasien 之前,kode_jk 值必须在 tb_jk 表中可用。
Make sure you insert into the main table before the sub table. You can also suppress foreign key check in your inserts.
就我而言,将设置了外键的字段名称添加到模型中的“allowed_fields”数组,解决了该问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.