I inserted records in database with auto generated id, now i want to get that id when i click on submit button that brings me multi file upload page
Asked
Active
Viewed 160 times
1
-
[this link](https://stackoverflow.com/questions/16440366/how-to-get-last-insert-id-after-insert-query-in-codeigniter-active-record) – danish-khan-I Aug 02 '19 at 05:38
-
`$this->db->insert_id()` is last inserted it – Devsi Odedra Aug 02 '19 at 05:44
3 Answers
0
Try this
function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();
return $insert_id;
}
In case of multiple inserts you could use
$this->db->trans_start();
$this->db->trans_complete();

Serghei Leonenco
- 3,478
- 2
- 8
- 16
-
use `$assignment_id=$this->db->insert_id();` not `$this->db->assignment_id();` – Devsi Odedra Aug 02 '19 at 05:45
-
I want to pass that id on other page using redirect, is that possible? – Happy Christian Aug 02 '19 at 05:53
-
-
But better way to use session variables, if you work just in web development – Serghei Leonenco Aug 02 '19 at 05:56
-
did it like this: `redirect('Welcome/file_upload/assignment_id='.$res);` $res contains that auto generated id – Happy Christian Aug 02 '19 at 05:57
-
I can not tell you for sure, post you controller, where you you call the method, and check the way how you making a redirection complying to codeignitor pattern – Serghei Leonenco Aug 02 '19 at 06:00
0
$this->db->insert_id();
This will give you last inserted id after inserting record.

Abhijit
- 105
- 2
- 14