如何从字段auto_increment填充字段外键

时间:2017-03-20 18:48:55

标签: php mysql database codeigniter mysqli

我有第一个表用户的3个表,$ data我有$ id和它的auto_increment,它就像自动填充mysql但问题是第二个表,第三个$ id是来自第一个表的外键,当我从第二个表插入数据,$ id,第三个表是空的,如何从第二个表和第三个表获得$ id for fill $ id,这里是代码:

if($this->form_validation->run() == TRUE){
            echo 'Harap Tunggu...';
            //data akun
            $email = $this->input->post('email');
            $pass = md5($this->input->post('password'));

            //data pribadi
            $nama = $this->input->post('nama_lengkap');
            $alamat = $this->input->post('alamat');
            $tgl_lhr = $this->input->post('date');
            $no_telp = $this->input->post('no_telp');

            //data pendidikan
            $tingkat_pendidikan = $this->input->post('tingkat_pendidikan');
            $nama_institusi = $this->input->post('namainstitusi');
            $jurusan = $this->input->post('jurusan');
            $no_induk = $this->input->post('nomor_induk');
            $nilai = $this->input->post('nilai');
            $no_telpon_institusi = $this->input->post('no_institusi');

            $salt = $this->salt();

            $pass = $this->makePassword($this->input->post('password'), $salt);


            $data = array(
                'id' => $id,
                'email' => $email,
                'password' => $pass,
                'salt' => $salt
            );

            $data2 = array(
                'nama_lengkap' => $nama,
                'tanggal_lahir' => $tgl_lhr,
                'alamat' => $alamat,
                'no_telpon' => $no_telp,
                'id' => $id
                );

            $data3 = array(
                'tingkat_pendidikan' => $tingkat_pendidikan,
                'nama_institusi' => $nama_institusi,
                'jurusan' => $jurusan,
                'no_induk' => $no_induk,
                'nilai' => $nilai,
                'no_telpon_institusi' => $no_telpon_institusi,
                'id' => $id
                );


            $this->db->insert('user',$data);
            $id = $this->db->insert_id();
            $data2['id'] = $data3['id'] = $id;
            $this->db->insert('data_pribadi',$data2);
            $this->db->insert('pendidikan',$data3);

            $this->session->set_flashdata("success","your account has been created");
            $this->session->set_flashdata('id', $id);
            redirect('upload_foto/index','refresh');

        }

2 个答案:

答案 0 :(得分:0)

要获取插入的最后一个ID,您可以使用以下代码。 致电$this->db->lastInsertId(); check this link

答案 1 :(得分:0)

  1. 我不知道$这意味着什么,但PDO和mysqli实现lastInsertId功能
  2. 您应该调用第一个插入,然后将最后插入的id存储到变量中。
  3. 使用lastInsertId方法的返回值分配$ data2 ['id']和$ data3 ['id']。
  4. 代码:

     <?php
           if($this->form_validation->run() == TRUE){
                echo 'Harap Tunggu...';
                //data akun
                $email = $this->input->post('email');
                $pass = md5($this->input->post('password'));
    
                //data pribadi
                $nama = $this->input->post('nama_lengkap');
                $alamat = $this->input->post('alamat');
                $tgl_lhr = $this->input->post('date');
                $no_telp = $this->input->post('no_telp');
    
                //data pendidikan
                $tingkat_pendidikan = $this->input->post('tingkat_pendidikan');
                $nama_institusi = $this->input->post('namainstitusi');
                $jurusan = $this->input->post('jurusan');
                $no_induk = $this->input->post('nomor_induk');
                $nilai = $this->input->post('nilai');
                $no_telpon_institusi = $this->input->post('no_institusi');
    
                $salt = $this->salt();
    
                $pass = $this->makePassword($this->input->post('password'), $salt);
    
    
                $data = array(
                    'id' => $id,
                    'email' => $email,
                    'password' => $pass,
                    'salt' => $salt
                );
    
                $data2 = array(
                    'nama_lengkap' => $nama,
                    'tanggal_lahir' => $tgl_lhr,
                    'alamat' => $alamat,
                    'no_telpon' => $no_telp,
                    'id' => $id // Undefined
                    );
    
                $data3 = array(
                    'tingkat_pendidikan' => $tingkat_pendidikan,
                    'nama_institusi' => $nama_institusi,
                    'jurusan' => $jurusan,
                    'no_induk' => $no_induk,
                    'nilai' => $nilai,
                    'no_telpon_institusi' => $no_telpon_institusi,
                    'id' => $id // Undefined
                    );
    
    
                $this->db->insert('user',$data);
                $id = $this->db->insert_id();
                $data2['id'] = $data3['id'] = $id;
                $this->db->insert('data_pribadi',$data2);
                $this->db->insert('pendidikan',$data3);
    
                $this->session->set_flashdata("success","your account has been created");
                $this->session->set_flashdata('id', $id);
                redirect('upload_foto/index','refresh');
    
            }