MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 7th, 2000, 05:39 AM
atom71 atom71 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 2 atom71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi
I have a set of data that is being inserted into two tables but i want to catch the new ID (auto incremented) of the first table and place its value in a field with the second part of the data in the second table.

how do i do this with PHP & MYSQL?
And how do i ensure that another set of data hasn't ben inserted in the first table meantime?

I want do do something like - but dont know correct syntax or even if it could ever work.

INSERT INTO content (title,description,typeid,userid,dateadded) VALUES ('$title','$description','$typeid','$userid','$date')

then

INSERT INTO links (url,sitename,contentid) VALUES ('a',b',SELECT LAST_INS
ERT_ID() FROM content);

your expert help would be greatly apreciated to help me understand this stuff

Thanks

Reply With Quote
  #2  
Old October 7th, 2000, 07:19 AM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,385 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 73048 Folding Title: Intermediate FolderFolding Points: 73048 Folding Title: Intermediate FolderFolding Points: 73048 Folding Title: Intermediate FolderFolding Points: 73048 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 21 h 11 m 18 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Do your first insert like you've got, then

$last_id = mysql_insert_id($link_id);

to get the number of the last id. then do the insert into your second table, using $last_id where ever you need it. You don't need the $link_id if you only have a connection open to one database.

---JH

------------------
*************************************************************
* The manual can probably answer 90% of your questions... *
* *
* PHP Manual. www.php.net/manual *
* MySQL Manual: www.mysql.com/documentation/mysql/bychapter *
*************************************************************

Reply With Quote
  #3  
Old October 7th, 2000, 07:30 AM
atom71 atom71 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 2 atom71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks, That is a great help.

I guess I was RT WRONG FM ;-) - will check both next time.

Angus

Reply With Quote
  #4  
Old October 7th, 2000, 12:18 PM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,385 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 73048 Folding Title: Intermediate FolderFolding Points: 73048 Folding Title: Intermediate FolderFolding Points: 73048 Folding Title: Intermediate FolderFolding Points: 73048 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 21 h 11 m 18 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Actually, you can do it in two sql statements also, I just gave it a try...

INSERT INTO content (title,description,typeid,userid,dateadded) VALUES ('$title','$description','$typeid','$userid','$date')

INSERT INTO links (url,sitename,contentid) VALUES ('a',b',LAST_INSERT_ID());

That's probably more what you were looking for...

---JH

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > What is correct way to INSERT data into 2 tables simultaneously?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT