943,915 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 786
  • PHP RSS
Nov 8th, 2008
0

how to share primary key?

Expand Post »
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
sql Syntax (Toggle Plain Text)
  1. 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 */
Last edited by peter_budo; Nov 8th, 2008 at 7:09 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Nov 8th, 2008
0

Re: how to share primary key?

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")

PHP Syntax (Toggle Plain Text)
  1. 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

PHP Syntax (Toggle Plain Text)
  1. $sql2='insert into phone (pid, phoneNum) values ("$ppid", "$phno")';
Last edited by peter_budo; Nov 8th, 2008 at 7:09 pm. Reason: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 8th, 2008
0

Re: how to share primary key?

Click to Expand / Collapse  Quote originally posted by jackakos ...
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")

PHP Syntax (Toggle Plain Text)
  1. 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

PHP Syntax (Toggle Plain Text)
  1. $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:

PHP Syntax (Toggle Plain Text)
  1. $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
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Nov 9th, 2008
0

Re: how to share primary key?

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

Try This
php Syntax (Toggle Plain Text)
  1. sql='insert into person (pid, lastname, firstname) values ("NULL", "$ln","$fn")'; // generate ID by inserting NULL

And this is for the second table
php Syntax (Toggle Plain Text)
  1. $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/...unique-id.html
Last edited by jackakos; Nov 9th, 2008 at 11:36 am. Reason: provide a link
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Nov 12th, 2008
0

Re: how to share primary key?

K2K,

can you close this by clicking on solved if your problem has indeed been solved?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: query behaving funny
Next Thread in PHP Forum Timeline: fck editor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC