Search code examples
phpmysqlwebuser-accounts

Updating relational database in PHP/MySQL


I am teaching myself about web programming. I am trying to create a user account for a website. I have two databases - first one (db_users - userid (auto increment), username, password) to store user information like the username, password, etc. and second one (db_address - userid (auto increment), address1, address2, city) to store the user's address.

Now when I register a user, how do I ensure that both the tables are updated with the correct user id? Or is that not how a relational database works?


Solution

  • Without going into code you want to do:

    1. Insert details into user table
    2. Get ID inserted using $id = mysql_insert_id()
    3. Insert details into address table using $id

    EDIT: If using more than one MySQL connection you need to do $id = mysql_insert_id($connection)