I am creating a personnel recruitment website and am having an issue with adding new data to a record created on the previous page.
For ease of use, I have split the form into a number of steps (each step being on a new page), where Step 1 creates the record in the candidate database table and inserts the candidate’s personal details (name, surname, contact details, gender, etc.) and sends the user to the next page (Step 2), where an profile pic can be uploaded and the path to the image is inserted into a field in the newly created record.
The problem comes in that I can’t seem to get the code to call the record ID of the relevant record. I thought of using mysql_insert_id but the issue with that is twofold. Firstly, I’m not sure if I know how to use this correctly and multiple Google searches on this have left me none the wiser. Secondly, as the company will have multiple branches around the country, there is a likelihood that two users could be adding candidates at the same time and User A could end up adding the photo to the record that User B created.
I have tried using $_POST to get the record ID but the data doesn’t seem to be passing the data to the next page once it has been inserted into the table.
I am not exactly new to PHP but this is not something I have done before because I used to have a friend who would handle this kind of thing but he, unfortunately, passed away a few months ago so I am having to learn how to do this as I go along, with little success.
I hope I am being clear enough about my problem because I am at a bit of a loss.
Any assistance would be greatly appreciated.
First of all, I would use PDO (or mysqli) insetead of the deprecated mysql_* extensions.
Using PDO, after you execute your statement, you can use two functions to get the ID you need:
By the way, I use
rowCount()because I normally useON DUPLICATE KEY UPDATEon most of my insert queries, you could possibly skip that step.Then I would store the ID in a session
or add it as a hidden field for the next form.By the way, depending on the requirements, I would not store anything in a db just yet until all required information has come in.