I have a UserLogin table for users to login with username and password. I also have a UserInfo table containing Address, Phone Numbers, and information in 30 other columns.
When a user is created in the UserLogin table, I want to match up their UserLogin row with a row in the UserInfo table so that I can store user details in the UserInfo table. My goal is to have these tables have Primary Key IDs that match so that the UserLoginID = '2493' and UserInfoID = '2493'. That way, the tables will match row for row, one for one, in efforts that the user profile information will always marry up with their UserLogin account.
What is the best way (best practices) to accomplish this?
UserLogin Table
- userID (Primary Key)(Auto_Increment)
- username
- password
- email
- useraccess
UserInfo Table
- UserInfoID (Primary Key)(Auto_Increment)
- FirstName
- LastName
- Address
- PhoneNumber
When a new UserID is created, I want a UserInfoID to also be created and tied to the UserID so that when they login and enter in their Profile details, it will enter into the UserInfo table.
Thx to Vintage Coders! This is what I ended up w/ and it works flawlessly:
$stmt = $db->prepare('INSERT INTO UserLogin (username,password,email)
VALUES (:username, :password, :email)');
$stmt->execute(array(
':username' => $username,
':password' => $hashedpassword,
':email' => $email ));
$id = $db->lastInsertId('adminID');
include('includes/database.php');
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
$mysqli->query("INSERT INTO UserInfo(UserInfoID, FirstName, LastName)
VALUES ('$id', '$FirstName', '$LastName')");
There are two solutions:
1) When you insert the record of first table then you can get the last inserted id and you save your second table UserInfoID as getting from the first table.
2) You can create a new field in the second table i.e related to the first table (userid). and when you insert the record in the second table ,you can insert last inserted id of the first table add in second table column i.e (userid). The formula is :
mysql_insert_id();