| | |
how to share primary key?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2007
Posts: 227
Reputation:
Solved Threads: 0
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
/*
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 */
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)
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.
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")
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
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)
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)
$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.
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Keep PEACE alive!
•
•
Join Date: Nov 2007
Posts: 227
Reputation:
Solved Threads: 0
•
•
•
•
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)
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)
$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)
$pid = mysql_query("SELECT pID FROM person WHERE firstName= '$_POST(firstName)', '$_POST(middleName)', '$_POST(lastName)'");
K2K, sorry for not being clear enough but the procedure I meant was last_insert_id()
Try This
And this is for the second table
Hope this works or you can read much about it yourself
http://dev.mysql.com/doc/refman/4.1/...unique-id.html
Try This
php Syntax (Toggle Plain Text)
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)
$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
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Keep PEACE alive!
![]() |
Similar Threads
- HELP WITH SOUND CARD - 82801FB ICH6 AC'97 Audio Controller - (PCI and Add-In Cards)
- database design advice (Database Design)
- Dual hard drives (Storage)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- E R Modeling--Basic concepts (Computer Science)
- code for login and password..using vb6 (Visual Basic 4 / 5 / 6)
- Query hell (MySQL)
Other Threads in the PHP Forum
- Previous Thread: query behaving funny
- Next Thread: fck editor
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode up-to-date update upload url validator variable video web xml youtube





