![]() |
Having trouble with $this->db->insert_id() - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Having trouble with $this->db->insert_id() (/showthread.php?tid=16265) |
Having trouble with $this->db->insert_id() - El Forum - 03-01-2009 [eluser]Chad Crowell[/eluser] I have a working php/mssql/odbc CI install on 1.7.1. I'm just in the beginnings of a new app. I've got this function in my controller that inserts a kit title into the db table and *should* return the unique ID from the table and allow me to redirect the user to the edit page for this new kit. Here is the code: Code: function add() { The insert is happening with no problems - I can see the new kit every time I test this when I go back to the kit listing page, but I am not seeing a $kit_id echoe'd to the add page and I am not being forwarded to the edit page (I realize I don't need a physical add page but i was using it for testing). I did have to chase down a couple of bugs for the odbc driver for mssql but its been working great. Maybe related, though? Having trouble with $this->db->insert_id() - El Forum - 03-01-2009 [eluser]JayTee[/eluser] Even when you comment out the redirect, you're not seeing the id, right? Have you tried just using the "SELECT @@IDENTITY" to bypass the CI active-record class to see if you can even get it back? Having trouble with $this->db->insert_id() - El Forum - 03-02-2009 [eluser]TheFuzzy0ne[/eluser] Check that the primary key column has the auto increment attribute set. Having trouble with $this->db->insert_id() - El Forum - 03-02-2009 [eluser]Chad Crowell[/eluser] @JayTee Yes, without the redirect, nothing shows on the page. Thanks will try that- though I'd love to remain in AR if I can @TheFuzzyOne - yes that is set. This is a pre-existing ASP site that I am converting to CI Having trouble with $this->db->insert_id() - El Forum - 03-02-2009 [eluser]TheFuzzy0ne[/eluser] I know this sounds obvious, but can you confirm that your if statement is actually being hit? I once forgot that I added an underscore to the name attribute in the view, and spent ages trying to figure out what was wrong. Of course, now it's the first place I look when I get problems similar to this. Having trouble with $this->db->insert_id() - El Forum - 03-02-2009 [eluser]Chad Crowell[/eluser] I found some info on the web that the odbc_insert_id command from PHP doesn't work too well, so I decided not to fight it and went with this, which works fine as this is a low transaction site. Code: function add() { Having trouble with $this->db->insert_id() - El Forum - 04-18-2012 [eluser]misteryosox44[/eluser] If this still helps, you can also use this method for getting the insert_id; $res = $this->db->query("SELECT @@IDENTITY as insert_id")->row_array(); echo $res['insert_id']; //the last inserted id //you may place it right after your insert statement. Having trouble with $this->db->insert_id() - El Forum - 04-18-2012 [eluser]CroNiX[/eluser] Why is everyone responding to 3 year old posts tonight? lol Having trouble with $this->db->insert_id() - El Forum - 04-18-2012 [eluser]misteryosox44[/eluser] hahaha. sorry for that , just new in this forum, and experienced the same problem. Newbie programmer here ![]() |