I have a table Products and a table Files.
Each product may have from 0 to 10 or more files associated with it. So it
is a 1 to M relationship...
My problem is that I cannont see how I can associate files to the product at
the time of Product insertion to the DB.
And this is because I don't know the ID taht the product is going to take
before it is inserted into the DB.
So any IDEAS ?
Thanks Angelos
> And this is because I don't know the ID taht the product is going to take
> before it is inserted into the DB.
First insert the product, then get the id for it using last_insert_id()
and then insert rest of the rows, using that id. (C, PHP and other
implementations have direct function for that, with ODBC you need to run
new query to get it).
And the id last_insert_id() returns is safe to use even with many users,
because it is attached to the current connection. So user with another
connection wont get the id you used in another connection.
C:
http://dev.mysql.com/doc/mysql/en/getting-unique-id.html
PHP:
http://fi2.php.net/manual/fi/function.mysql-insert-id.php
ODBC:
http://dev.mysql.com/doc/mysql/en/odbc-and-last-insert-id.html
> PHP:
> http://fi2.php.net/manual/fi/function.mysql-insert-id.php
THanks a lot man ....
Thats great news !!!!