943,070 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1405
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 20th, 2009
0

One DB record from two HTML forms

Expand Post »
I have two HTML pages, each has a form.
The user is supposed to fill out the input fields in one, then click a submit button that will send him to the next page, where he would fill out the rest of the fields of his questionaire, click a submit button.
After that I need all the input data collected from both pages (forms) to be inserted into one record in the database.
How would I achieve that?

Thanks a million in advance!
Last edited by levsha; Nov 20th, 2009 at 5:53 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
levsha is offline Offline
137 posts
since Nov 2009
Nov 20th, 2009
1
Re: One DB record from two HTML forms
You can do it in 2 simple ways (maybe there are even more, but I am not aware of it).
You can either save the data of the first form in session after it is posted, and then, when the second form is posted, get all the data (session data and the 2nd form's posted data) and insert a record to the table.
The second method is to have hidden fields in the 2nd form, which will save all the values of the first form and when the 2nd form is submitted, you will have the data of 1st and 2nd form
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Nov 20th, 2009
0
Re: One DB record from two HTML forms
Hey.

You could use sessions to store the data from the first part of the form until the second one is submitted.

Or you could insert the data from the first page into the database as soon as it is submitted, have the user fill out the second part, and just update the row when the second part is submitted.

For example:

index.php
html Syntax (Toggle Plain Text)
  1. <form action="page2.php" method="post">
  2. <input type="text" name="first_info" />
  3. <input type="submit" />
  4. </form>

page2.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Get the data from the last form
  3. $first_info = mysql_real_escape_string($_POST['first_info']);
  4.  
  5. // Insert a new row into the database
  6. $sql = "INSERT INTO tbl(first_info) VALUES('{$first_info}')";
  7. mysql_query($sql) or trigger_error(mysql_error(), U_USER_ERROR);
  8.  
  9. // Get the ID of the row that was just created.
  10. $row_id = mysql_insert_id();
  11. ?>
  12. <form action="page3.php" method="post">
  13. <input type="hidden" name="row_id" value="<?php echo $row_id; ?>" />
  14. <input type="text" name="second_info" />
  15. <input type="submit" />
  16. </form>

page3.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Get the data from the last form
  3. $second_info = mysql_real_escape_string($_POST['second_info']);
  4. $row_id = mysql_real_escape_string($_POST['row_id'])
  5.  
  6. // Update the row in the database with the new info
  7. $sql = "UPDATE tbl
  8. SET second_info='{$second_info}'
  9. WHERE row_id={$row_id}
  10. LIMIT 1";
  11. mysql_query($sql) or trigger_error(mysql_error(), U_USER_ERROR);
  12. ?>
  13. <form action="page4.php" method="post">
  14. <input type="hidden" name="row_id" value="<?php echo $row_id; ?>" />
  15. <input type="text" name="third_info" />
  16. <input type="submit" />
  17. </form>
etc...
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 20th, 2009
0
Re: One DB record from two HTML forms
Quote ...
Or you could insert the data from the first page into the database as soon as it is submitted, have the user fill out the second part, and just update the row when the second part is submitted.
The only problem with this is , if a user loses his interest midway [ie., after submitting 1st form], there will be a obsolete record in the table. Or else, this is good.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Nov 20th, 2009
0
Re: One DB record from two HTML forms
Click to Expand / Collapse  Quote originally posted by nav33n ...
The only problem with this is , if a user loses his interest midway [ie., after submitting 1st form], there will be a obsolete record in the table. Or else, this is good.
True, that might be a problem. Although I have never really found to much data to be a problem
It could even help you refine your process; being able to see if a lot of people are abandoning it mid way, and exactly where.

I would still consider this marginally better than storing it all in a session. Bloated sessions are never a good thing. It would even be preferable, in my opinion, to use a database as temporary storage for things like this, rather than the session.

And I would never really consider re-posting it all back into the HTML between pages. That's just... not right, somehow. I would much rather create one big form and have client-side code simulate a multi-page submission.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 20th, 2009
0
Re: One DB record from two HTML forms
$row_id = mysql_insert_id()

This is it!
Since I am a newbee to PHP, I just didn't know how I could identify the row.
But inserting data from the first page right away and then updating the record with data from the second page was my initial idea!

Thank you so much!
Reputation Points: 10
Solved Threads: 0
Junior Poster
levsha is offline Offline
137 posts
since Nov 2009
Nov 21st, 2009
0
Re: One DB record from two HTML forms
I've tried something else, but it didn't work.
I have an identity (auto-increment) field 'inventorid' in my table.
When data from the first form is submitted, the row is inserted into the table and a new inventorid value is generated.
I put this into my PHP on the submittion of the second page:
PHP Syntax (Toggle Plain Text)
  1. $sql = "UPDATE inventors SET invname='$invname'
  2. WHERE inventorid='(SELECT max(inventorid) FROM inventors )'";

Somehow it doesn't work...
Reputation Points: 10
Solved Threads: 0
Junior Poster
levsha is offline Offline
137 posts
since Nov 2009
Nov 21st, 2009
0
Re: One DB record from two HTML forms
Click to Expand / Collapse  Quote originally posted by levsha ...
I've tried something else, but it didn't work.
I have an identity (auto-increment) field 'inventorid' in my table.
When data from the first form is submitted, the row is inserted into the table and a new inventorid value is generated.
I put this into my PHP on the submittion of the second page:
PHP Syntax (Toggle Plain Text)
  1. $sql = "UPDATE inventors SET invname='$invname'
  2. WHERE inventorid='(SELECT max(inventorid) FROM inventors )'";

Somehow it doesn't work...
actually the previous way is better... you should retrieve the inventorid from mysql_insert_id() function immediately after the insertion and keep track of ti. I will tell you the reason.
Imagine a user fills in the 1st part of form and the data gets entered into the DB. Now, while he is busy with filling 2nd part another user logs in and fills the 1st part of form which increments the inventorid. Now when u r trying to update 1st user's record with max(inventorid) it will give u the id of 2nd user and update it.. after that second user will do the same and overwrite those entries. This way the 2nd part of 1st user gets lost..
got my point??
Reputation Points: 13
Solved Threads: 21
Junior Poster
venkat0904 is offline Offline
186 posts
since Oct 2009
Nov 21st, 2009
-2
Re: One DB record from two HTML forms
This is a situation where ajax could help. You really don't need to have two pages. You could have just the one with a DIV that is updated to show different forms. This means you could have a number of different steps without changing the page.
Sponsor
Featured Poster
Reputation Points: 1036
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,617 posts
since Oct 2006
Nov 21st, 2009
0
Re: One DB record from two HTML forms
Yes, it makes perfect sense.
Thank you!

Click to Expand / Collapse  Quote originally posted by venkat0904 ...
actually the previous way is better... you should retrieve the inventorid from mysql_insert_id() function immediately after the insertion and keep track of ti. I will tell you the reason.
Imagine a user fills in the 1st part of form and the data gets entered into the DB. Now, while he is busy with filling 2nd part another user logs in and fills the 1st part of form which increments the inventorid. Now when u r trying to update 1st user's record with max(inventorid) it will give u the id of 2nd user and update it.. after that second user will do the same and overwrite those entries. This way the 2nd part of 1st user gets lost..
got my point??
Reputation Points: 10
Solved Threads: 0
Junior Poster
levsha is offline Offline
137 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC