iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-06-2009, 03:32 PM
WebProWorld New Member
 
Join Date: Jul 2009
Posts: 1
edon12 RepRank 0
Default php insert different records into multiple mysql tables with same unique id

Hi everyone!

I have what is probably a simple problem with php. I'm relatively new to using php.

I am creating the registration page for my website and I want multiple tables to be filled into mysql using different data. However, I still need the unique id number to be filled into the tables and to be the same number in every table for a single user.

My id fields are int(11) unsigned auto_increment - this field is the primary key in the tables I am updating. I am able to generate the unique id number for my first table by simply using NULL in php so mysql will assign the next highest unique id number. This works for the INSERT for the first table but then how to I INSERT thta same unique id number in another table using a separate INSERT command right after the first one?

If there are the same number of records in each table, this is no problem. But, if one table gets out of order and has more records than the other, the unique id numbers do not align correctly between the separate tables.

Thanks in advance for any help, I'm really stuck on this.
Reply With Quote
  #2 (permalink)  
Old 07-06-2009, 07:10 PM
thindenim's Avatar
WebProWorld Pro
 
Join Date: Jan 2007
Location: Scotland
Posts: 256
thindenim RepRank 2
Default Re: php insert different records into multiple mysql tables with same unique id

Hi edon,

You can use the mysql_insert_id function (PHP mysql_insert_id() Function) to grab the id of the last inserted record. Note that below I am using a database class.

Your php would be something like: -

$db->query("INSERT INTO tbl_1 (column1, column2) VALUES ('abc','xyz');"); // 1st table insertion
$id = mysql_insert_id; // get the id of the last inserted record
$db->query("INSERT INTO tbl_2 (id, column3) VALUES (".$id.", 'def');"); // 2nd table insertion

I would however recommend having a different id field relating to the primary table for each of the secondary tables, so you would have your unique auto incremented id, and also tbl1_id - something like that. So the third line would become: -

$db->query("INSERT INTO tbl_2 (tbl1_id, column3) VALUES (".$id.", 'def');"); // 2nd table insertion

Hope this makes sense!

David
__________________
Girlz Night - professional hair and beauty products
Web design glasgow - from Thin Denim
Reply With Quote
  #3 (permalink)  
Old 07-06-2009, 08:35 PM
DaveSawers's Avatar
WebProWorld Veteran
 
Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 492
DaveSawers RepRank 3DaveSawers RepRank 3
Default Re: php insert different records into multiple mysql tables with same unique id

Makes sense to me.

Basically, if you use auto_increment on several tables, you cannot make the assumption that they will stay in step. One solution would be to take the auto_increment off all but the primary table and use mysql_insert_id to get the id from the primary table and then put that id into the subsequent tables as described above.

If there's a chance that id's in subsequent tables won't be unique, then just remove the primary_key flag from that field and use the table without a key.
__________________
Dynamic Software Development
www.activeminds.ca
Reply With Quote
  #4 (permalink)  
Old 07-07-2009, 01:06 PM
thindenim's Avatar
WebProWorld Pro
 
Join Date: Jan 2007
Location: Scotland
Posts: 256
thindenim RepRank 2
Default Re: php insert different records into multiple mysql tables with same unique id

yep, that's exactly it dave. Either take the primary key and auto increment off, or use another field. Personally I would use another field.
__________________
Girlz Night - professional hair and beauty products
Web design glasgow - from Thin Denim
Reply With Quote
  #5 (permalink)  
Old 07-07-2009, 01:26 PM
wige's Avatar
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,661
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: php insert different records into multiple mysql tables with same unique id

Well, the matter isn't only the autoincrement, you also need to know the id assigned to the main record so you can populate the remaining tables. So, if you have a customer table, and a customer address table, the customer address will have a primary key, but it still needs to know the id of the corresponding record in the customer table.

When you create the record in the first table, you can get the primary key assigned to the record with the following PHP line:
PHP Code:
$pkey mysql_insert_id(); 
__________________
The best way to learn anything, is to question everything.
Reply With Quote
  #6 (permalink)  
Old 07-07-2009, 02:05 PM
WebProWorld New Member
 
Join Date: Jul 2003
Location: Susquehanna, PA
Posts: 10
Clay Martin RepRank 0
Default Re: php insert different records into multiple mysql tables with same unique id

What has been said here is dead on, I would add one other thought. On the secondary tables (that have a field populated with the key (call this the prime id) generated for the first table that you used mysql_insert_id() to get the auto incremented key) let those tables have their own unique key, it is useful for doing updates later. Also change the field that contains the generated id(prime key) to be a index for the other tables. This will speed access when you take the prime key from the first table and go to collect the related info from the other tables.

HTH
Clay
Reply With Quote
  #7 (permalink)  
Old 07-22-2009, 08:13 PM
WebProWorld New Member
 
Join Date: Jul 2009
Posts: 16
Les N00bian RepRank 0
Default Re: php insert different records into multiple mysql tables with same unique id

I'm so glad I came across this post, it was exactly my question too. I have a follow up question that is related to this problem. I too have a registration form, and after that is written to the database, I want to direct the user to 2 or 3 subsequent pages to enter demographic and other data. How do I redirect the user to the second page when the form is submitted, and will the id I grabbed from mysqli_insert_id carry over to subsequent pages, or will this need to be saved as a session variable?

Thanks you guys rock.
Reply With Quote
  #8 (permalink)  
Old 07-22-2009, 10:03 PM
DaveSawers's Avatar
WebProWorld Veteran
 
Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 492
DaveSawers RepRank 3DaveSawers RepRank 3
Default Re: php insert different records into multiple mysql tables with same unique id

You can use the action element of the form to direct them to the next page, fill in the next form and then move to the third page the same way.

You can either put the result of the mysql_insert_id into the link ...\page2.php?id=... or use session variables.
__________________
Dynamic Software Development
www.activeminds.ca
Reply With Quote
  #9 (permalink)  
Old 07-23-2009, 03:34 AM
WebProWorld New Member
 
Join Date: Jul 2009
Posts: 16
Les N00bian RepRank 0
Default Re: php insert different records into multiple mysql tables with same unique id

Thanks for the reply. I'm trying to post a sample code so I can see where I'm going wrong but the forum's giving me problems saying I'm not allowed to post links or emails until after 10 posts. If anyone wouldn't mind looking I can email or PM the sample. Really appreciate all the help, already getting much further than I was previously.
Reply With Quote
  #10 (permalink)  
Old 07-23-2009, 04:22 AM
williamc's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: Jul 2003
Location: GoogleVille
Posts: 1,585
williamc RepRank 7williamc RepRank 7williamc RepRank 7williamc RepRank 7williamc RepRank 7williamc RepRank 7williamc RepRank 7williamc RepRank 7williamc RepRank 7
Default Re: php insert different records into multiple mysql tables with same unique id

Quote:
Originally Posted by DaveSawers View Post
You can use the action element of the form to direct them to the next page, fill in the next form and then move to the third page the same way.

You can either put the result of the mysql_insert_id into the link ...\page2.php?id=... or use session variables.
As an addendum to Daves points, most people use a form field for this ie:

<input type="hidden" name="id' value="<?php echo $_REQUEST[id]; ?>">
__________________
William Cross
Expert Search Engine Optimization
Reply With Quote
  #11 (permalink)  
Old 07-23-2009, 06:15 PM
WebProWorld New Member
 
Join Date: Jul 2009
Posts: 16
Les N00bian RepRank 0
Default Re: php insert different records into multiple mysql tables with same unique id

Okay, sorry for the dumb question but here goes. I'm able to grab that ID now using the mysqli_insert_id($dbc), however if I use the action in the form to go to the next page, the data doesn't get written to the table. It only gets written when I have the form action direct back to itself.
Reply With Quote
  #12 (permalink)  
Old 07-23-2009, 08:00 PM
DaveSawers's Avatar
WebProWorld Veteran
 
Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 492
DaveSawers RepRank 3DaveSawers RepRank 3
Default Re: php insert different records into multiple mysql tables with same unique id

So you need to put the code that writes to the database at the top of page 2, not in page 1. You can then write page 2 to pass on the id to page 3 using a hidden form field or whatever.
__________________
Dynamic Software Development
www.activeminds.ca
Reply With Quote
  #13 (permalink)  
Old 07-30-2009, 06:47 PM
WebProWorld New Member
 
Join Date: Jul 2009
Posts: 16
Les N00bian RepRank 0
Default Re: php insert different records into multiple mysql tables with same unique id

First of all, this has been so much help. My application is in a basic functioning state, so thank you so much. Where I've run into a problem, is validating data on page 1/form 1, when form 1 is not actually posted until page 2 (per your instructions above). For example, I am trying to make sure that a username is not already in use before allowing a new users info to post to the database, but I can't validate that on form 1, without posting back to itself, and I can't validate it on page 2 because by then the form action has already loaded the next form.

Suggestions?
Reply With Quote
  #14 (permalink)  
Old 09-02-2009, 09:19 AM
WebProWorld New Member
 
Join Date: Jun 2008
Posts: 6
nowreturn RepRank 0
Default Re: php insert different records into multiple mysql tables with same unique id

mysql_insert_id();
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
mySQL Select 7 random records,e ach from 1 category getmea Database Discussion Forum 3 08-25-2008 03:51 PM
Multiple insert into more than one mySQL table davss Web Programming Discussion Forum 1 06-15-2007 06:37 PM
Displaying records - PHP/MYSQL seorocks Web Programming Discussion Forum 1 12-22-2006 09:45 AM
Relational Tables in MySQL dchuke Database Discussion Forum 2 10-10-2004 06:40 AM
delete or iserting multiple records ferhanz Database Discussion Forum 1 03-12-2004 12:47 PM


All times are GMT -4. The time now is 08:45 AM.



Search Engine Optimization by vBSEO 3.3.0