Insert into two table with one transaction with second table using the ID generated from the first

using mysql and pdo is it possible to insert into one table and then insert into the second table inserting the id generated from the first query in one transaction?

INSERT INTO item (creatorId, name) VALUES (:m, "this is  a test);
INSERT INTO details ( creatorId, itemId, term, content ) VALUES ( :m, LAST_INSERT_ID(), "owner", :m );
INSERT INTO colors( creatorId, itemId, colors ) VALUES ( :m, LAST_INSERT_ID(), "green");

Open in new window


How can i refer to the first insert and get that ID?
LVL 6
J NUnicorn wranglerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
Why dont you  store that value in a variable and reuse it;
0
J NUnicorn wranglerAuthor Commented:
Is it possible to create a variable in an sql statement? such as $var = INSERT INTO....?
0
Aneesh RetnakaranDatabase AdministratorCommented:
SET @var1 := last_insert_id()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

J NUnicorn wranglerAuthor Commented:
Would this need to be in a stored procedure?
0
Aneesh RetnakaranDatabase AdministratorCommented:
I always prefer stored procedures over Inline sql statements.  Otherwise it has to be a single dynamic sql statement.
0
nociSoftware EngineerCommented:
sorry, not quite the correct answer.

To make it an transaction a COMMIT is needed..... between them, if history is unknown then:

COMMIT; // allprevious outstanding...
INSERT INTO item (creatorId, name) VALUES (:m, "this is  a test);
SET @var1 := last_insert_id()
INSERT INTO details ( creatorId, itemId, term, content ) VALUES ( :m, :var1, "owner", :m );
INSERT INTO colors( creatorId, itemId, colors ) VALUES ( :m, :var1, "green");
COMMIT; // store.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.