简体   繁体   English

MySQL:无法添加或更新子行:在 Codeigniter 中将我的数据插入数据库时​​,外键约束失败

[英]MySQL : Cannot add or update a child row: a foreign key constraint fails when insert my data to database in Codeigniter

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.我收到这样的错误:无法添加或更新子行:使用 Codeigniter 将我的数据插入数据库 phpmyadmin 时,外键约束失败。

This is my error message :这是我的错误信息:

Cannot add or update a child row: a foreign key constraint fails ( tugasakhir . tb_pasien , CONSTRAINT tb_pasien_ibfk_1 FOREIGN KEY ( kode_pasien ) REFERENCES tb_jk ( kode_jk ) ON DELETE CASCADE ON UPDATE CASCADE)无法添加或更新子行:外键约束失败( tugasakhir tb_pasien ,CONSTRAINT tb_pasien_ibfk_1 FOREIGN KEY( kode_pasien )REFERENCES tb_jkkode_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) 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_pasien,@60dokua'70dokua'70dokua'40dokuac'80dokuac'80dokuac'80dokuac'80dokuac'8dokuac', 70dokuac', 70n.come '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

我的 tb_jk 的价值
Note : kode_jk is foreign key on tb_pasien table.注意: kode_jk 是 tb_pasien 表上的外键。
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.注意:在 FOREIGN KEY 和 REFERENCES 中指定的子表和父表的列数必须相同,数据类型也必须相同。

Table 'tb_pasien' :表 '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”数组,解决了该问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL:插入和更新时出错:无法添加或更新子行:外键约束失败 - MySQL: Error when insert and update: Cannot add or update a child row: a foreign key constraint fails Mysql:无法添加或更新子行:外键约束失败 - Mysql:Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败(mysql中的外键问题) - Cannot add or update a child row: a foreign key constraint fails(Foreign key issue in mysql) 无法添加或更新子行:外键约束失败(Mysql和外键) - Cannot add or update a child row: a foreign key constraint fails (Mysql and Foreign key) LARAVEL:MYSQL:违反完整性约束:1452无法添加或更新子行:外键约束失败 - LARAVEL:MYSQL : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 添加外键约束时无法更新子表:错误:无法添加或更新子行:外键约束失败 - unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails 错误:无法添加或更新子行:外键约束失败? - Error: Cannot add or update a child row: a foreign key constraint fails? 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails “无法添加或更新子行:外键约束失败”的问题 - Problem with “Cannot add or update a child row: a foreign key constraint fails”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM