Welcome Guest, Not a member yet? Register   Sign In
inserting into multiple tables from one form
#1

[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.
#2

[eluser]danmontgomery[/eluser]
Code:
$this->db->insert_id();

Returns the last auto-generated id.
#3

[eluser]Todlerone[/eluser]
TY noctrum. How do I determine the proper table that it gets the value from?
#4

[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.
#5

[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();
You can then start inserting a new record into the second (schedule) table setting the value of the field you are using to contain your foreign key to the created variable.




Theme © iAndrew 2016 - Forum software by © MyBB