Returning the Primary Key when inserting a record |
[eluser]juddmuir[/eluser]
Hi, I'm using $this->db->insert('table_name', $data); I then want to save some images uploaded at the same time, using the primary key of the inserted record as the folder name. What's best practice for getting hold of the Primary Key after inserting a record? Thanks!
[eluser]Michael Wales[/eluser]
Use a class variable within your model to store the Unique ID on a successful insertion. Your model: Code: class Post extends Model { Your Controller: Code: class Posts extends Controller {
[eluser]juddmuir[/eluser]
Aha, that's what I was looking for: $this->db->insert_id(); Not documented as far as I can see, and very useful! Thanks for the reply!
[eluser]Colin Williams[/eluser]
http://ellislab.com/codeigniter/user-gui...lpers.html |
Welcome Guest, Not a member yet? Register Sign In |