Forum Moderators: coopster
I'm looking for some advice and/or best practices to accomplish the following task.
I have a web form that users will complete and submit to be saved to a database [php/mysql]. The problem is that I need to split up the rather large form into three separate pages. When the user submits the first section, the database will auto-generate a unique id for that row...
How do I then obtain that unique id from the database on the submit, in order to pass it to the next page, in order that I insert the information from page two into the same row as the information from page 1.
I hope that you all can understand my dilemna. Any help will be greatly appreciated.
Thank you in advance,
Mike
$ID = $rowID ['ID'];
/////////////////////////////////
////pass it to next form//////////
<p><strong>Please procceed to <a href="hotels2.php?name=<? echo $name?>&ID=<? echo $ID?>">STEP 2</a> </strong></p>
............in steps 2 and 3 u just update the row of the database accordingto the ID....
Hope it is clear....if u need clarfications plz post....
//process query
$result = mysql_query($query);
if ($result)
{$newID = mysql_insert_id();}
then just use that $newID in your update functions to put the rest of the data into the database
hth, jenny
Getting the id of the last entered record [webmasterworld.com]
Can anyone quell this with a more in depth explanation of mysql_insert_id?
I have a doubt on this and I couldn't find the answer in any forum.Assuming that several users are filling in the form at the same time, then which ID would the mysql_insertID function return? Or would it just return the ID inserted by that particular user?
From the mysql documentation:
"The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions."
For more info:
http: //www.php.net/manual/en/function.mysql-insert-id.php
I now have a new question regarding security based on this. As you already know, I'm trying to create a form that spans across two pages. I'm using the mysql_insert_id() function to get the unique ID created from the insert on the first page in order that I can do a sql update on the second page and keep all the information for the two page form in one record in the database.
Therefore, the sql statement on page two is something like 'UPDATE SET VAR1 = $VAR1, VAR2 = $VAR2 WHERE id = $id'
Here is what I need to know,
I obviously can't use a querystring to pass the id obtained using mysql_insert_id() from page 1 to page 2. This is because anyone could change the querystring id to x and update row x in my database.
I run into the problem here of not knowing the best practice to hide this information as I pass it from part 1 to part 2 in order to make it impossible for the end user (of whatever skill level) to modify it and start screwing up my database
My initial thoughts lead me to think that I could use Posting (as opposed to GET), Session variable, or cookies.
What is the best practice/path for me to take to secure this process?
Thank you
Mike
However using the php session management code is much simpler if your host allows it.
You may also want to unset the session variable after you send the data to the database so it doesn't submit again if the user refreshes the page or if they do the form over again.
The above method also protects against things like if someone submits the form on page 1 and goes to page 2. Then hits the back button and submits the form on page 1 again, you don't want to create a new entry in the database. Think of scenarios like this when error protecting your pages.
here's a synopsis of the problem:
I have a form that I need to split over 2 pages, but the data needs to remain as 1 row in the database. I DO want to collect partial data (i.e. I DO want page 1, even if they don't complete page 2) so Page 1 needs to be a mysql Insert, and Page 2 an Update.
My solution was to do the following:
On page 1 the user fills out the form and clicks submit.
Php inserts the user input data into the table along with the sessionID variable and a timestamp
Mysql/php returns the row # for the insert, which I then pass to page 2 of the form via querystring
On page 2 the user fills out more form data and clicks submit.
I then use the querystring id to read the sessionID variable from the db and compare it to the current sessionID variable. This way, someone can't randomly change the querystring id and start updating rows that dont' belong to them (the issue i'm trying to avoid).
Barring this description making no sense to anyone but me, I would love to know what the experts in this form think of my solution methodology.
Thank you,
Mike