0

I have 2 tables: user and photos (in a mysql db).

Here you can see the relation between the 2 tables

    User                          Photos
-----------------   -------------------------------
|  id  |  user  |   |   id_user   |   photo_url   |
-----------------   -------------------------------
   ||                      /\   
   ||______________________||
   |________________________| 

When I save a new user into the table "user", if the insert is done successfully, I have to take the value of the "id" field of the user just saved, and save it inside the "id_user" field of the table "photos", in order to make a relation between a user and its photos.

Making the first insert (table "user"), then getting, with a SELECT, the "id" of the last record and then again saving the user's photos it's not the right way to do it. How should i do it?

MDP
  • 4,177
  • 21
  • 63
  • 119

2 Answers2

0

To get the auto-incremented ID for the previous insert, you'll want to use mysql_insert_id().

Chris Barcroft
  • 562
  • 5
  • 12
0

use this toobtain user id: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

30chars

dimzon
  • 414
  • 4
  • 13