Maybe not the best topic, but I am trying to make a simple example out of this in order to explain it more clearly. My tables in the example are:
CREATE TABLE users_logindata (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(250) UNIQUE DEFAULT NULL,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE user_info(
id INT AUTO_INCREMENT PRIMARY KEY,
id_user INT,
adress VARCHAR(255),
name VARCHAR(255),
country VARCHAR(255),
city VARCHAR(255),
postal_place VARCHAR(255),
zip_code VARCHAR(255)
);
I have one table that contain the login data of the user, and another table that hold the "extra" data the user might fill in if he wants to. But all can be filled in using one signup-form!
The script first create the user by adding the given data (required) to a database. In order to get the id that references to the user_info table (that store the extra data )i need to get the id from the 'users_logindata'. And use the id as reference inside the 'user_info' table.
The code to get the last id:
/* get last entry in db*/
public function getLastEntry($tablename){
if ($result = $this->connect()->db_connection->query("SELECT id FROM $tablename ORDER BY id DESC LIMIT 1" )) {
while ($o = $result->fetch_object()) {
return $o->id;
}
}
}
Steps would be something like:
1) add the user to 'users_logindata'
2) get the id by selecting the last id from 'users_logindata'
3) Use the value from step 2 and add it into the 'user_info => id_user' table.
Then my question is: if i have alot of users signing up at the same time, is it then possible that the id might be mixed up? since one user can reg between the adduser query (step1) and the fetching of the id (step2). Since im getting the last ID, that actually might be another id since there are more signing up at once?, or does PHP handle one .php file at a time?