One DB record from two HTML forms

Thread Solved

Join Date: Nov 2009
Posts: 36
Reputation: levsha is an unknown quantity at this point 
Solved Threads: 0
levsha levsha is offline Offline
Light Poster

One DB record from two HTML forms

 
0
  #1
Nov 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,809
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 341
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
1
  #2
Nov 20th, 2009
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
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 502
Reputation: Atli is on a distinguished road 
Solved Threads: 62
Atli's Avatar
Atli Atli is offline Offline
Posting Pro
 
0
  #3
Nov 20th, 2009
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
  1. <form action="page2.php" method="post">
  2. <input type="text" name="first_info" />
  3. <input type="submit" />
  4. </form>

page2.php
  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
  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...
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,809
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 341
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
0
  #4
Nov 20th, 2009
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.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 502
Reputation: Atli is on a distinguished road 
Solved Threads: 62
Atli's Avatar
Atli Atli is offline Offline
Posting Pro
 
0
  #5
Nov 20th, 2009
Originally Posted by nav33n View Post
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.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 36
Reputation: levsha is an unknown quantity at this point 
Solved Threads: 0
levsha levsha is offline Offline
Light Poster
 
0
  #6
Nov 20th, 2009
$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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 36
Reputation: levsha is an unknown quantity at this point 
Solved Threads: 0
levsha levsha is offline Offline
Light Poster
 
0
  #7
Nov 21st, 2009
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:
  1. $sql = "UPDATE inventors SET invname='$invname'
  2. WHERE inventorid='(SELECT max(inventorid) FROM inventors )'";

Somehow it doesn't work...
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 185
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 20
venkat0904's Avatar
venkat0904 venkat0904 is offline Offline
Junior Poster
 
0
  #8
Nov 21st, 2009
Originally Posted by levsha View Post
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:
  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??
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,353
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 172
ardav's Avatar
ardav ardav is offline Offline
Nearly a Posting Virtuoso
 
-2
  #9
Nov 21st, 2009
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.
Twpsyn cythraul. Cawr y dom tarw. Gwybod dim, gofyn dim.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 36
Reputation: levsha is an unknown quantity at this point 
Solved Threads: 0
levsha levsha is offline Offline
Light Poster
 
0
  #10
Nov 21st, 2009
Yes, it makes perfect sense.
Thank you!

Originally Posted by venkat0904 View Post
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??
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 907 | Replies: 14
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC