Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/SQL adding user id into a look-up table for new user
    primarykey
    data
    text
    <p>I have a script which adds a new user to the database and stores them in the users table. I also have a contracts table (a list of contracts) and a look-up/linking table for users to contracts.</p> <p>I want to know the best way to add a new user to the contracts table when you will not know what the unique ID they will be given is?</p> <p>Example:</p> <p><strong>step 1</strong> - Sign-up form has normal user inputs, username, password ect and a number of checkboxes for contracts (all contracts have a unique ID also).</p> <p><strong>step 2</strong> - the form is submitted and users details are added to the user table and a unique ID is generated for that new user.</p> <p><strong>step 3</strong> - the contracts selected in step 1 are inserted into the users_contracts table using the unique ID of the contracts selected and the unique ID of the new user.</p> <p>This is where im having a brain block. How do I know what the new users unique ID will be?</p> <p>The only way I can think at the moment is to INSERT the new user as normal, then do another query for the new username and pull the new users ID from the database then INSERT it in to the users_contracts table in a third query.</p> <p>Something like:</p> <pre><code>INSERT INTO `users` _POST DATA FROM FORM </code></pre> <p>Then get the above INSERTED user and store in a variable:</p> <pre><code>SELECT FROM users _POST USERNAME </code></pre> <p>Then add it to the users_contracts table:</p> <pre><code>INSERT INTO users_contracts _POST contracts and the above created variable for the users ID </code></pre> <p>SORRY should mention im using PDO for the insert and all database activity. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload