Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: Dec 2008
Posts: 3
#1: 2 Weeks Ago
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!
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#2: 2 Weeks Ago

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


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!
Reply