ainoy31 Posted June 15, 2007 Share Posted June 15, 2007 Hello- I am trying to get the logic in inserting data from a registration form into two tables in mysql database. One table (name) has a primary key (name_id) and the other (address) has a primary (addr_id) and foreign key (name_id). Inserting into the tables is not a problem but I want to make sure that if I reference to the name and address table, it will retrieve the probably address with the name. Do I have to first insert into the name table and then get what the name_id is? Next, insert that name_id into the address table along with the rest of the info? I hope this is clear enough. Much appreciation. thx. AM Link to comment https://forums.phpfreaks.com/topic/55711-inserting-data-from-a-form-into-two-tables/ Share on other sites More sharing options...
Nhoj Posted June 15, 2007 Share Posted June 15, 2007 You could do something like: mysql_query('INSERT INTO `users` VALUES (NULL, "NAME")'); $name_id = mysql_insert_id(); mysql_query('INSERT INTO `addresses` VALUES (NULL, '.$name_id.', "'.$address.'")'); Edit: The NULL values represent the columns that would probably be auto-incremented... Link to comment https://forums.phpfreaks.com/topic/55711-inserting-data-from-a-form-into-two-tables/#findComment-275262 Share on other sites More sharing options...
ainoy31 Posted June 15, 2007 Author Share Posted June 15, 2007 I see what you are suggesting. One thing though, I am trying to understand this statement: $name_id = mysql_insert_id(); Link to comment https://forums.phpfreaks.com/topic/55711-inserting-data-from-a-form-into-two-tables/#findComment-275272 Share on other sites More sharing options...
ainoy31 Posted June 15, 2007 Author Share Posted June 15, 2007 thanks for the help. Link to comment https://forums.phpfreaks.com/topic/55711-inserting-data-from-a-form-into-two-tables/#findComment-275286 Share on other sites More sharing options...
MrSheen Posted June 15, 2007 Share Posted June 15, 2007 I see what you are suggesting. One thing though, I am trying to understand this statement: $name_id = mysql_insert_id(); I think it gets the ID of the last inserted row. Link to comment https://forums.phpfreaks.com/topic/55711-inserting-data-from-a-form-into-two-tables/#findComment-275290 Share on other sites More sharing options...
ainoy31 Posted June 15, 2007 Author Share Posted June 15, 2007 Yeah. All is working good. Link to comment https://forums.phpfreaks.com/topic/55711-inserting-data-from-a-form-into-two-tables/#findComment-275300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.