0

My CI_Controller

public function insert()
{

   $insert1 = array('reg_name'=>$this->input->POST("name"),
                   'reg_mob'=>$this->input->POST("mobile"),
                   'reg_address'=>$this->input->POST("address"));
   $insert2 = array('uname'=>$this->input->POST("uname"),
                    'upass'=>$this->input->POST("pass"));
   $this->Insert_model->insertData($insert,$insert2);
 }    

CI Model

public function insertData($insert1,$insert2)
{
    $result1 = $this->db->insert("register",$insert1);
    $reg_id=$this->db->insert_id();
    $result2 = $this->db->insert("login",$insert2);
    $id=$this->db->insert_id();
    $data=array( 'reg_id' => $reg_id, 'id' => $id );
    echo $this->db->insert('login', $data);
 } 

I have two table Registration and login reg_id is fk in login table i have error Duplicate entry '5' for key 'id'

INSERT INTO `login` (`reg_id`, `id`) VALUES (45, 5)    
David
  • 5,882
  • 3
  • 33
  • 44
ankita
  • 1
  • What are you trying to achieve by inserting into `login` table twice consecutively? Why not insert all data at once? – Alex Sep 07 '18 at 20:21

1 Answers1

0

Try this; you are inserting twice in to login with the same values causing a duplicate key.

public function insertData($insert1,$insert2)
{
    $this->db->insert("register", $insert1);
    $insert2['reg_id'] = $this->db->insert_id();
    $this->db->insert("login", $insert2);
 }

Change db column id in login to auto_increment. Also would be helpful to truncate the db tables to start fresh.

Alex
  • 9,215
  • 8
  • 39
  • 82