![]() |
inserting into multiple tables from one form - 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: inserting into multiple tables from one form (/showthread.php?tid=35842) |
inserting into multiple tables from one form - El Forum - 11-12-2010 [eluser]Todlerone[/eluser] Hello everyone and TGIF if it applies to you. I have a form/database question. I have a form that has about 10 questions. Three of them are dates. What I would like to do is have them be inserted into a different table then the form submits to. I want them to go to a schedule table. My problem is the form submits to a table that sets the person id with an auto increment. How do I get the id to foreign link the schedule dates to this id number? TY in advance. inserting into multiple tables from one form - El Forum - 11-12-2010 [eluser]danmontgomery[/eluser] Code: $this->db->insert_id(); Returns the last auto-generated id. inserting into multiple tables from one form - El Forum - 11-12-2010 [eluser]Todlerone[/eluser] TY noctrum. How do I determine the proper table that it gets the value from? inserting into multiple tables from one form - El Forum - 11-12-2010 [eluser]danmontgomery[/eluser] [quote author="Todlerone" date="1289619750"]TY noctrum. How do I determine the proper table that it gets the value from?[/quote] It comes from whatever table the last insert statement was executed on. You can run $this->db->last_query() to see the query, though I don't see how that would be programmatically useful, you should be calling $this->db->insert_id() immediately after an insert query, anyways. inserting into multiple tables from one form - El Forum - 11-12-2010 [eluser]matt.asbury[/eluser] Hi Todlerone And TGIF indeed. I assume you have a field in the second table to contain the foreign key from the first table? All you would need to do, as noctum suggested, is to insert the set of fields you need into the first table (I assume things like name) and then after you have inserted the record into the database, you set a variable for your new foreign key using the insert_id() function: Code: $foreign_key = $this->db->insert_id(); |