Auto creating a database record and using that records ID throughout the app. |
[eluser]Unknown[/eluser]
Hello, I'm trying to create an application that when the user first arrives at the main viewer they are asked to input their name and click next. When they click next a new row is added to a database and the page refreshes to a new view that asks another question. This question needs to be added to the same row as the first question. My problem is getting the $ID of the the record that was created in the first screen. I don't understand how to create a record to the database then share that records ID through out each controller/view for the remainder of the users experience in the application. I'm sorry if this is very basic as I new to CI Thanks
[eluser]guidorossi[/eluser]
It's the mysql_insert_id() function to get the last insert id. It's $this->db->insert_id() from the query helper functions on CodeIgniter
[eluser]LuckyFella73[/eluser]
Additionally to what guidorossi posted you can use the session class and store the insert_id into a session variable. That way you can access the database id all the way long. I allways use database session, have a look at the user guide to get the sql code and to see how to enable DB sessions.
[eluser]Unknown[/eluser]
I'm having problems with the insert_id() tag reporting 0 each time I call it. I've read several other posts on here about the same issue and I really think my problem is my poor implementation of said code. I've not looked into the session class option yet as it looks like it would help a lot. I'm trying to carry this id across several different controllers "sharing the variable" so I can update the table row with the data entered in each view. I hope this makes sense. I feel this should be fairly simple but just can't crack this. thanks,
[eluser]guidorossi[/eluser]
You should use it like this: Code: $inserdata = array(
[eluser]tonanbarbarian[/eluser]
i would probably also test to see if the insert is working Code: $inserdata = array(
|
Welcome Guest, Not a member yet? Register Sign In |