I’m building a web application in work and I have a form where users can register certain types of information.
Username | Password | Company
Now I’m unsure how to approach this. What I want is when the user submits that registration form Username, password and company get written to one data table(user) BUT because Company, in the user data table, is a foreign key reference I need Company to be written to a separate datatable(company) as a Primary Key (and of course username to be written as a FK reference as its a 1 – 1 relationship).
I’m not looking for a coded solution from you guys because I know my PHP and MYSQL I’m just looking for some pseudo code algorithms to get the creative juices flowing!
EDIT: I AM USING POSTGRESQL not MYSQL but I’m pretty sure there’s little difference except port numbers and small syntax changes
assume first column is
id:$mysqli->query("INSERT INTO company VALUES (NULL, '$company_name')";$company_id = $mysqli->insert_id;$mysqli->query("INSERT INTO user VALUES (NULL, '$username', '$password', '$company_id')";$user_id = $mysqli->insert_id;$mysqli->query("UPDATE company SET user_id = $user_id WHERE company_id = $company_id)";