Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[HTML | CSS | PHP] Help Desk | Need help with Homework Or Just stuck at something?

OP, how can I insert into multiple tables whilst keeping the ID's the same?
What I'm trying to achieve is simple.

I would like to insert all my products into one table, and then insert all my prices into another table, while keeping track of the ID for each product, so that I can then insert them into the "id" field of the prices (which is not a primary key).
(Dec 28, 2014 11:22 PM)Isildur Wrote:  Assuming you are using php.
$query1 = "INSERT INTO ..... ";
$YourDBDriver->query( $query1 );

If you use mysql. Then, you can fetch the last inserted id (Primary key) by:
$id = mysql_insert_id();

Different drivers use different methods. When using PDO, lastInsertId() method can be used.

and then use this with the second query
$query2 = "INSERT INTO `Table` ( primaryid, id, OTHERFIELDS ) VALUES ( NULL, {$id}, OTHERFIELDS )";
$YourDBDriver->query( $query2 );

The same can also be achieved using SQL only, inwhich you need to execute multiple queries at the same time.
For this you need to use the CLIENT_MULTI_STATEMENTS constant (orr numeric value 65536) in your mysql connect function.

Then you can just use:

Code
INSERT INTO Tablename ( some_fields ) VALUES ( some_values );
SET @last_id = LAST_INSERT_ID();
INSERT INTO Othertable ( id, some_fields ) VALUES (@last_id, some_fields);
Yes, that would be the correct answer - except I can't hammer away at the database with individual SQL queries when entering thousands of products.

Can anyone help? 😅
I recently created a html5 index and I'm using IP.Board as a forum. The things i want to achieve:

- I want to use the same header and footer from my index on my forum as well.
- I want to use the same BG for both my index and forum
- I want to make the forum width a little bit narrower

I know it are only tiny changes i've summed up above but since i'm pretty inexperienced a little help will be much appreciated!


Possibly Related Threads...
Thread / Author Replies Views Last Post
by HackersSep 28, 2026 01:50 PM 9 363 Sep 28, 2026 01:50 PM
Last Post: JuWacky
by oxagastSep 3, 2022 07:52 AM 0 197 Sep 3, 2022 07:52 AM
Last Post: oxagast
by AerrowsGirlfriendJun 16, 2021 04:17 AM 1 319 Jun 16, 2021 04:17 AM
Last Post: Nachfolger