Reputation: 1129
I have this code, what I want to do is to get the primary key lastly inserted into the user table and then put that into the foreign key of the library table. I don't know how to do it with $request or what to do here?
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$data1 = array(
'user_id' => '5',// this is where I have to put the primary key from last table
'library_name' => $request['lib_name']
);
$this->model->insert('user', $data);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
Upvotes: 0
Views: 573
Reputation: 1129
Thanks guys, it worked all i needed to do was just to change the logic, I was using it stupidly and didn't see where to place the insert query so Its working now all i had to do was just to change the place of the insert query as given
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$this->model->insert('user', $data);
$data1 = array(
'user_id' => mysql_insert_id(),// now it works as insert has been done above ....
'library_name' => $request['lib_name']
);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
Upvotes: 0
Reputation: 509
you can do it like this
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$data1 = array(
'library_name' => $request['lib_name']
);
$this->model->insert('user', $data);
$user_id = mysql_insert_id();
$data1['userid'] = $user_id;
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
} If you are using any frameworks. then they have the helper functions to get last insert id, you should use them
Upvotes: 0
Reputation: 5380
-- Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.
Upvotes: 1
Reputation: 4631
You can do like this
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$this->model->insert('user', $data);
$user_id = $this->model->select('user', $data); //select user_id from db
$data1 = array(
'user_id' => $user_id ,
'library_name' => $request['lib_name']
);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
Upvotes: 0