By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,694 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,694 IT Pros & Developers. It's quick & easy.

Form that submits data to three tables, need help linking two tables to one

P: 3
I want one form to submit to 3 different tables in MYSQL... I got them to submit to the right tables, but I want to take the first tables id (primary key, auto_increment) and make that the realtor_id of the other two tables. Anyone know what I need to put in the field of the other 2 to make it read it properly? Heres what I have so far...

Expand|Select|Wrap|Line Numbers
  1. if($_POST['submit'])
  2. {
  3.  
  4. $sql = "INSERT INTO logins (username, password) VALUES('" . $_POST['usernameBox'] . "', '" . $_POST['passwordBox'] . "');";
  5. mysql_query($sql);
  6.  
  7. $realtorsql = "INSERT INTO realtors (realtor_id, first_name, last_name, address, city, state, zip, email, phone, cell, fax) VALUES( '" . $sql['id'] . "', '" . $_POST['firstnameBox'] . "', '" . $_POST['lastnameBox'] . "', '" . $_POST['addressBox'] . "', '" . $_POST['cityBox'] . "', '" . $_POST['stateBox'] . "', '" . $_POST['zipBox'] . "', '" . $_POST['emailBox'] . "', '" . $_POST['phoneBox'] . "', '" . $_POST['cellBox'] . "', '" . $_POST['faxBox'] . "');";
  8. mysql_query($realtorsql);
  9.  
  10. $websitesql = "INSERT INTO realtors_websites (realtor_id, main_title) VALUES('" . $sql['id'] . "', '" . Welcome . "');";
  11. mysql_query($websitesql);
  12.  
  13. header("Location: " . $config_basedir . "realtorwelcome.php");
  14. }
Obviously '" . $sql['id'] . "' doesn't work, what would I use instead? Or should I make the first page just username/ pass and next page it loads up the additional information, but I don't know how to get it to read the new id formed on the next page in that scenario either. Thank you!
Nov 4 '09 #1
Share this Question
Share on Google+
1 Reply


100+
P: 201
use last_insert_id() in the insert string for the other tables for that foreign key to the parent table.

what that does is insert the parent primary key into the foreign key id on the child table.

that will be for each session of the app/query.

hope that helps!
Nov 4 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.