| | |
One DB record from two HTML forms
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 36
Reputation:
Solved Threads: 0
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!
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.
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
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*
*PM asking for help will be ignored*
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
page2.php
page3.php
etc...
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)
<form action="page2.php" method="post"> <input type="text" name="first_info" /> <input type="submit" /> </form>
page2.php
php Syntax (Toggle Plain Text)
<?php // Get the data from the last form $first_info = mysql_real_escape_string($_POST['first_info']); // Insert a new row into the database $sql = "INSERT INTO tbl(first_info) VALUES('{$first_info}')"; mysql_query($sql) or trigger_error(mysql_error(), U_USER_ERROR); // Get the ID of the row that was just created. $row_id = mysql_insert_id(); ?> <form action="page3.php" method="post"> <input type="hidden" name="row_id" value="<?php echo $row_id; ?>" /> <input type="text" name="second_info" /> <input type="submit" /> </form>
page3.php
php Syntax (Toggle Plain Text)
<?php // Get the data from the last form $second_info = mysql_real_escape_string($_POST['second_info']); $row_id = mysql_real_escape_string($_POST['row_id']) // Update the row in the database with the new info $sql = "UPDATE tbl SET second_info='{$second_info}' WHERE row_id={$row_id} LIMIT 1"; mysql_query($sql) or trigger_error(mysql_error(), U_USER_ERROR); ?> <form action="page4.php" method="post"> <input type="hidden" name="row_id" value="<?php echo $row_id; ?>" /> <input type="text" name="third_info" /> <input type="submit" /> </form>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
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.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
0
#5 Nov 20th, 2009
•
•
•
•
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.

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!
And use [code] tags!
•
•
Join Date: Nov 2009
Posts: 36
Reputation:
Solved Threads: 0
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!
•
•
Join Date: Nov 2009
Posts: 36
Reputation:
Solved Threads: 0
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:
Somehow it doesn'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)
$sql = "UPDATE inventors SET invname='$invname' WHERE inventorid='(SELECT max(inventorid) FROM inventors )'";
Somehow it doesn't work...
0
#8 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:
PHP Syntax (Toggle Plain Text)
$sql = "UPDATE inventors SET invname='$invname' WHERE inventorid='(SELECT max(inventorid) FROM inventors )'";
Somehow it doesn't work...
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
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
-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.
•
•
Join Date: Nov 2009
Posts: 36
Reputation:
Solved Threads: 0
0
#10 Nov 21st, 2009
Yes, it makes perfect sense.
Thank you!
Thank you!
•
•
•
•
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??
![]() |
Similar Threads
- Hide/Show html forms using javascript (JavaScript / DHTML / AJAX)
- Should HTML forms be data or view (PHP)
- Plz help me out for the source code in java for login-logout (Java)
- html/php form for .htaccess validation (PHP)
- News Story: HTML: The Next Generation (XML, XSLT and XPATH)
- How to use two forms in asp (ASP)
- Generation html in perl (Perl)
- Html+PHP Forms (PHP)
- msql data transer problems (MySQL)
- Can't send HTML forms (Web Browsers)
Other Threads in the PHP Forum
- Previous Thread: how do i display date()(the date from that function) in different timezones?
- Next Thread: How to convert a site in html/css to PHP?
Views: 907 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for PHP
access ajax apache api array arrays beginner binary broken cakephp check checkbox class cms code combobox cron curl database date development directory display download dropdown dropdownlist echo email error file files form forms function functions google header hosting href htaccess html image include insert integration ip java javascript joomla jquery limit link list login mail menu methods mlm mod_rewrite multiple mysql oop parse password paypal php problem query random recursion regex results rewrite script search select server sessions sms soap sorting source sql storage structure syntax system table tutorial unicode update updates upload url validation variable video web xml youtube






