Reputation: 1
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)
Upvotes: 0
Views: 44
Reputation: 9265
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.
Upvotes: 0