|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 * ************************************************************* |
|
#3
|
|||
|
|||
|
Thanks, That is a great help.
I guess I was RT WRONG FM ;-) - will check both next time. Angus |
|
#4
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > What is correct way to INSERT data into 2 tables simultaneously? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|