hi,
if i have a db:
person> pID (not null, auto_increment), lastname, firstname
phone>pID (not null), phoneNum, pID(foreign key points to "person")

assume connection is established and pointing to the right database already//////

//assume $ln and $fn have value for user input

mysql_query("INSERT INTO person( '  ', '$_POST[$ln]', '$_POST[$fn]' )");

/*

if the form page has to be filled with both person and phone table information
then how do i insert for "phone" wit the pID in the person table ?????
pID is auto-increment so it is not needed to be entered by the form filler.. how do i get that particular pID from the same person the form filler is trying enter data into?

sorry, so confusing */

From my understanding you have a form that needs to be filled but this form has both details of PERSON table and also the PHONE table.

Now, you want to know how to insert these details into the respective tables?

If the above scenarios are true, then, since you designed the form - you obviously have variables to accept the user inputs say $fn for First_Name something like that??

From your post these will be your tables:
person> pID (not null, auto_increment), lastname, firstname
phone>pID (not null), phoneNum, pID(foreign key points to "person")

sql='insert into person (pid, lastname, firstname) values ("NULL", "$ln","$fn")';

you can use a variable to accept the last pid from MySQL and use that variable as input to the second table ie. supposing you use $ppid

$sql2='insert into phone (pid, phoneNum) values ("$ppid", "$phno")';

From my understanding you have a form that needs to be filled but this form has both details of PERSON table and also the PHONE table.

Now, you want to know how to insert these details into the respective tables?

If the above scenarios are true, then, since you designed the form - you obviously have variables to accept the user inputs say $fn for First_Name something like that??

From your post these will be your tables:
person> pID (not null, auto_increment), lastname, firstname
phone>pID (not null), phoneNum, pID(foreign key points to "person")

sql='insert into person (pid, lastname, firstname) values ("NULL", "$ln","$fn")';

you can use a variable to accept the last pid from MySQL and use that variable as input to the second table ie. supposing you use $ppid

$sql2='insert into phone (pid, phoneNum) values ("$ppid", "$phno")';

jackakos,
your scenario is absolutely what I am working on. However, since pid is auto_increment, i don't need the user to input the pid but pid will automatically be inserted. That is where the problem starts. My try was:

$pid = mysql_query("SELECT pID FROM person WHERE firstName= '$_POST(firstName)', '$_POST(middleName)', '$_POST(lastName)'");

// so basically i try to select the pID from the person table where the person's firstName, middleName and lastName match the user input. Well, of course it gave me errors. And i think since all the new data (fn, mn, ln and phone) will insert together, then how it will search the firstName middle... last.... while the data are not even there yet?... so i guess it is not the way... any suggestion?

K2K, sorry for not being clear enough but the procedure I meant was last_insert_id()

Try This

sql='insert into person (pid, lastname, firstname) values ("NULL", "$ln","$fn")';   // generate ID by inserting NULL

And this is for the second table

$sql2='insert into phone (pid, phoneNum) values (LAST_INSERT_ID(), "$phno")';

Hope this works or you can read much about it yourself
http://dev.mysql.com/doc/refman/4.1/en/getting-unique-id.html

K2K,

can you close this by clicking on solved if your problem has indeed been solved?

This article has been dead for over six months. Start a new discussion instead.