public function register() {
if (isset($_POST['register'])) {
$u = $_POST['uname'];
$this->load->database();
$this->load->database();
$this->db->select('uname');
$this->db->from('login');
$this->db->where(array('uname' => $u));
$query1 = $this->db->get();
if (!$query1->num_rows() == 1) {
$data = array(
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'dob' => $_POST['dob'],
'gender' => $_POST['gender'],
'email' => $_POST['email']
);
$this->load->database();
$this->db->insert('user', $data);
$data1 = array(
//'User_idUser'=>$_POST[$query1],
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);
$this->db->insert('login', $data1);
} else {
$_SESSION["ex"] = "User All Ready Exists";
}
}
$this->load->view('register');
}
我希望得到用户表的自动增量id,以便在登录表中保存为外键,以识别当前正在记录的用户。代码运行得很好。
用户表是父表和 登录表是子表。
答案 0 :(得分:0)
用户使用代码获取上次插入操作的ID。
$insert_id = $this->db->insert_id();
并使用$inser_id
将其添加到您想要的任何表格中。
因此,如果我打开代码的注释行,
$data1 = array(
'User_idUser' => $insert_id, // Assuming that UseridUser is your columne to store the Autoincremented user id from previous insert query.
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);