...

View Full Version : 2 page registration form



losse
06-15-2006, 02:56 PM
Hi there
I'm in a bit of a conundrum... I have a registration form that consists of 2 pages worth of user input. The first is the personal info like name and address and the second is the product/service they would like to register for.

Once the user clicks on "next" on page 1 of the registration, I send all the user inputs to the DB in an "INSERT" quiery. This creates an id for that user.

The problem is that when they input the stuff on page 2 it creates a SECOND user ID??

What can I do to the 2 page registration form so that all the inputs are tied to that user... instead of the registration form thinking that page 1 is for 1 user and page 2 is for another user?

Thanks

raf
06-15-2006, 05:02 PM
when you proces page 1, you make an insert and after the insert, you grab the primary key value of the inserted record. if your db is mysql, then you can use mysql_insert_id for that (check manual for example --> http://www.php.net/manual/nl/function.mysql-insert-id.php)
you then store that id in a sessionvariable. like:


$_SESSION['key_formID'] = mysql_insert_id();

when you proces page 2, you update the record that you created when you processed page 1, using the id in the where clause of your update.
Like:


$sql = "UPDATE yourtable SET foo=bar WHERE yourIDcolumn=". $_SESSION['key_formID'];

losse
06-15-2006, 05:08 PM
So page 1 would have the

$_SESSION['key_formID'] = mysql_insert_id();

and page 2 would have

$sql = "UPDATE yourtable SET foo=bar WHERE yourIDcolumn=". $_SESSION['key_formID'];

Correct??

What's foo-bar in this line:

$sql = "UPDATE yourtable SET foo=bar WHERE yourIDcolumn=". $_SESSION['key_formID'];


I get UPDATE yourtable

what would you put in SET foo-bar

and what would you put in WHERE yourIDcolum? Isn't that unknown at the time the user submits it ? Doesn't it have to be a variable depending on what the newly created ID is?

losse
06-15-2006, 05:50 PM
Ignore my previous post... I figured it out... Thanks for your patience!!!

I'm just a little iffy on the $_SESSION['key_formID']; portion...

Simply by putting that on the statement it will pick up the user ID that got created from the previuos page?

Thanks

losse
06-15-2006, 09:17 PM
Just a question is the 'key_formID' the name of the field that auto_increments?

For example , in my table the name of the field that is unique and auto-increments is called "ID" so does that mean that I would write these 2 lines as follows:


$_SESSION['ID'] = mysql_insert_id();

and


$sql = "UPDATE yourtable SET foo=bar WHERE yourIDcolumn=". $_SESSION['ID'];

anarchy3200
06-15-2006, 09:35 PM
No :p ...
The Session is basically just a variable that is carried by the browser rather than the page you can call this whatever you would like so...



$_SESSION['formID'] = mysql_insert_id();

$sql = "UPDATE yourtable SET foo=bar WHERE `ID`="._SESSION['formID'];


so the session part is similar to a variable called $formID just rather than being embedded in the page so as soon as you change page it is lost, it is held by the browser so as you change page until you either unset it or change it it will stay there.
The yourIDcolumn is where you need to put the column name for your primary key column (i put in in backticks but this is not necessary in this situation).

Also, just to make sure do you understand what goes in the foo=bar part aswell now?

losse
06-15-2006, 11:21 PM
Thanks mike
Yeah the foo-bar I think is what you're updating in what row... So WHERE calls the row that needs to updated and SET is the new data your inputing right?

My question is does the WHERE 'ID' actually get written like that or is that just a sample code???

How does the "WHERE" know to put the "SET" info within that user?

Thanks

anarchy3200
06-15-2006, 11:34 PM
If you primary key column is called ID then yes ...WHERE ID... is correct (if you do include the quote marks they should be backticks (Uk keyboard its the key one to the left of the numbers, they are quite often confused with single quotes)).


How does the "WHERE" know to put the "SET" info within that user?

Ok, it knows which column the information goes in because say your table has the following columns:

ID | firstname | surname | email

And the first page had only set the firstname field, so on the second page you need to insert the surname and email, your update query would be as follows:



$sql = "UPDATE yourtable SET `surname`='$surname', `email`='$email' WHERE `ID`="._SESSION['formID'];


I presume you can do the 'yourtable' part, this is just the name of your table.

losse
06-16-2006, 12:38 AM
Anarchy
Here's the code I currently have on page 1:



<?php
}
else {
// form submitted
// set server access variables
$host = "xxx";
$user = "xxx";
$pass = "xxx";
$db = "xxx";
// get form input
// check to make sure it's all there
// escape input values for greater safety
$product = empty($_POST['product']) ? die ("ERROR: Please enter the product you wish to have") : mysql_escape_string($_POST['product']);
$name = empty($_POST['name']) ? die ("ERROR: Please enter your first name") : mysql_escape_string($_POST['name']);
$last = empty($_POST['last']) ? die ("ERROR: Please enter your last name") : mysql_escape_string($_POST['last']);
$sin = empty($_POST['sin']) ? die ("ERROR: Please enter your Social Insurance Number") : mysql_escape_string($_POST['sin']);
$birth_day = empty($_POST['birth_day']) ? die ("ERROR: Please choose the day your were born on") : mysql_escape_string($_POST['birth_day']);
$birth_month = empty($_POST['birth_month']) ? die ("ERROR: Please choose the month you were born in") : mysql_escape_string($_POST['birth_month']);
$birth_year = empty($_POST['birth_year']) ? die ("ERROR: Please choose the year your were born in") : mysql_escape_string($_POST['birth_year']);
$home_tel = empty($_POST['home_tel']) ? die ("ERROR: Please enter your home phone number") : mysql_escape_string($_POST['home_tel']);
$email = empty($_POST['email']) ? die ("ERROR: Please enter your email address") : mysql_escape_string($_POST['email']);
$address = empty($_POST['address']) ? die ("ERROR: Please enter your street address") : mysql_escape_string($_POST['address']);
$city = empty($_POST['city']) ? die ("ERROR: Please enter the city you live in") : mysql_escape_string($_POST['city']);
$province = empty($_POST['province']) ? die ("ERROR: Please select your province") : mysql_escape_string($_POST['province']);
$postal = empty($_POST['postal']) ? die ("ERROR: Please enter your postal code") : mysql_escape_string($_POST['postal']);

$annual_report = (isset($_REQUEST['annual_report'])) ? 'YES' : 'NO';
$semi_report = (isset($_REQUEST['semi_report'])) ? 'YES' : 'NO';
$quarterly_report = (isset($_REQUEST['quarterly_report'])) ? 'YES' : 'NO';


// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "INSERT INTO client (product, annual_report, semi_report, quarterly_report, name, last, sin, birth_day, birth_month, birth_year, home_tel, biz_tel, email, address, apt, city, province, postal) VALUES ('$product', '$annual_report', '$semi_report', '$quarterly_report', '$name', '$last', '$sin', '$birth_day', '$birth_month', '$birth_year', '$home_tel', '$biz_tel', '$email', '$address', '$apt', '$city', '$province', '$postal')";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// redirect
header("Location: http://www.xxx.php");
ob_end_flush();

// close connection
mysql_close($connection);
}
?>


Would I simply put this line at the top of the script?


$_SESSION['formID'] = mysql_insert_id();

Then on page two of the registration from do as follows:


<?php
}
else {
// form submitted
// set server access variables
$host = "xxx";
$user = "xxx";
$pass = "xxx";
$db = "xxx";

// get form input
// check to make sure it's all there
// escape input values for greater safety
$maiden = empty($_POST['maiden']) ? die ("ERROR: Please choose your credit card company") : mysql_escape_string($_POST['maiden']);
$security_question = empty($_POST['security_question']) ? die ("ERROR: Please enter your credit card number") : mysql_escape_string($_POST['security_question']);

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query and get stuff from page 1
$sql = "UPDATE client SET `maiden`='$maiden', `security_question`='$security_question' WHERE `ID`="._SESSION['formID'];


// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// redirect
header("Location: http://www.xxx.php");
ob_end_flush();

// close connection
mysql_close($connection);
}
?>

anarchy3200
06-16-2006, 01:19 AM
I would put it after you run the insert so...


$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "INSERT INTO client (product, annual_report, semi_report, quarterly_report, name, last, sin, birth_day, birth_month, birth_year, home_tel, biz_tel, email, address, apt, city, province, postal) VALUES ('$product', '$annual_report', '$semi_report', '$quarterly_report', '$name', '$last', '$sin', '$birth_day', '$birth_month', '$birth_year', '$home_tel', '$biz_tel', '$email', '$address', '$apt', '$city', '$province', '$postal')";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$_SESSION['formID'] = mysql_insert_id();


As for the second page all looks ok.

losse
06-16-2006, 02:14 AM
Thanks Mike

What if I had 3 pages worth of registration form? Would I do the same thing I did with page 2 on page three?

By the way, congrats on the victory today...

anarchy3200
06-16-2006, 02:16 AM
Yeh, for the third page the session variable would still exist so you can just run another update using that, (that was the hard way of saying yes!)

Am i missing something, victory?

losse
06-16-2006, 02:24 AM
You mentioned something about the UK keyboard... Am I wrong in assuming your an England fan? They beat Trinidad and Tobago today in World Cup action...

Thanks for all your help... Will be testing it right now

anarchy3200
06-16-2006, 02:27 AM
Oh lol! Were now through but based on they're first game and the first half of this game there not going to get too far! :p

Glad to have helped, hope it all works ok (even though the actual code you needed was supplied by raf !)

losse
06-16-2006, 02:28 AM
d'oh!
I got this error


Parse error: parse error, unexpected '[' in /yadayada/register2.php on line 181

This is line 181:


$sql = "UPDATE client SET `maiden`='$maiden', `security_question`='$security_question' WHERE `ID`="._SESSION['formID'];

anarchy3200
06-16-2006, 02:32 AM
Sorry, that picked up a typo along the way, should be



$sql = "UPDATE client SET `maiden`='$maiden', `security_question`='$security_question' WHERE `ID`=".$_SESSION['formID'];

losse
06-16-2006, 03:55 AM
Thanks again...

What's this error all about??


Error in query: UPDATE client SET 'maiden'='smith', 'security_question'='question', 'security_answer'='answer', Check the manual that corresponds to your MySQL server version for the right syntax to use near ''maiden'='smith', 'security_question'='question', 'security_answer'='answer'

I got this when I was clicking to go to page 3 of the registration page. Going to page 2 worked perfectly fine (in terms of getting the data in the DB)

anarchy3200
06-16-2006, 04:03 AM
You remember what i said earlier about using backticks rather than quotes, well thats your problem!

you need: `
your using: '

For your columns you can just exclude them totally...
work with:


$sql = "UPDATE client SET maiden='$maiden', security_question='$security_question' WHERE ID=".$_SESSION['formID']

rather than


$sql = "UPDATE client SET `maiden`='$maiden', `security_question`='$security_question' WHERE `ID`=".$_SESSION['formID']

losse
06-16-2006, 04:53 AM
allright... I fixed that... and got this

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Now my line looks just like you put it...

Or should I put back ticks?

maiden='$maiden' or maiden=`$maiden`

anarchy3200
06-16-2006, 12:26 PM
Ok i'm still looking at why its not working but i'll just explain the backticks quickly, the backtics are a way of telling the sql server that it is a column name, the only reason i normally put them in is to aid readability as i will instantly know that they are the column names. Some situations may need them for slightly more obscure reasons but in the majority of cases, including yours they are not needed.
As you your question it should be
maiden='$maiden', or `maiden`='$maiden', not maiden=`$maiden`.

I'll now have a better look at the query for you!

anarchy3200
06-16-2006, 12:42 PM
Ok, just tried a couple of things and that error would imply that your update id is not being put in the query. Try commenting out the query and just printing the id to check if it is getting passed on, just to check you have got session_start(); at the top of the pages havn't you? (I don't think its come up on here but in theory page 2 wouldn't have worked without it so it must be there for at least the first two pages)

raf
06-16-2006, 12:55 PM
allright... I fixed that... and got this

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

the error most likely means that your sessionvariable isn't set or is empty.
(i can't explain to you how i know this since that would disclose illegal actions or me posessing superpowers)
if you cahnge your code to
[php]
$sql = "UPDATE client SET maiden='$maiden', security_question='$security_question' WHERE ID=".$_SESSION['formID']
die($sql);
[php]
then this will probably print
UPDATE client SET maiden='foobar', security_question='foobar' WHERE ID=
(where foobar is nerd-speak for 'some value'. foo=bar is nerd-speak for 'some variable/control becomes some value')

the reason probably is that you are new to using sessionvariales, and that you did not put
session_start();
at the top of the pages where you set or use a sessionvariable (manual --> http://www.php.net/manual/nl/function.session-start.php )

to answer your next post about an errormessage that "output has already been sent to the browser" that you get (again, don't ask me how i know this): the session_start() needs to be put at the very top of your page, between php-tags, before any output (not even a space or the pageheader or so) is sent.

losse
06-16-2006, 01:51 PM
Hi ...

Yeah I included a session_start(); enclosed in PHP tags first thing at the top of my page.


This is the error I get when I just try to echo the ID

Parse error: parse error, unexpected $ in /yadayadayada/register_test.php on line 171

Line 171 is the closing HTML tag on the page?!?!

losse
06-16-2006, 03:15 PM
Actually I thought of something?

Could the syntax be the isssue why this isn't working? This isn't a PHP 4.1 or 4.0 is it?

I've never really used that format of updating before...

raf
06-16-2006, 04:38 PM
This is the error I get when I just try to echo the ID

Parse error: parse error, unexpected $ in /yadayadayada/register_test.php on line 171

Line 171 is the closing HTML tag on the page?!?!
don't echo the ID. Echo the complete sql statement like in the code i posted
+ regarding the erromessage --> the linecount in these errormessage is often a few lines off, so start looking from line 171 upwards till you spot the problem, or else poste the complete code so we can see what's wrong.

i don't realy understand your version-question. what php version are you using? in older version, registering sessionvariables is indeed different, but we are talking here about versions that are quite a few years old...

losse
06-16-2006, 04:44 PM
Ok
Here's the entire code of the third registration page:


<?php
}
else {
// form submitted
// set server access variables
$host = "xxx";
$user = "xxx";
$pass = "xxx";
$db = "xxx";
// get form input
// check to make sure it's all there
// escape input values for greater safety
$maiden = empty($_POST['maiden']) ? die ("ERROR: Please choose your credit card company") : mysql_escape_string($_POST['maiden']);
$security_question = empty($_POST['security_question']) ? die ("ERROR: Please enter your credit card number") : mysql_escape_string($_POST['security_question']);
$security_answer = empty($_POST['security_answer']) ? die ("ERROR: Please select the month your card expires on") : mysql_escape_string($_POST['security_answer']);
$credit_confirm = empty($_POST['credit_confirm']) ? die ("ERROR: Please enter your credit card's validation number") : mysql_escape_string($_POST['credit_confirm']);

$credit_confirm = (isset($_REQUEST['credit_confirm'])) ? 'YES' : 'NO';

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query and get stuff from page 1 and 2
$query = "UPDATE client SET 'maiden'='$maiden', 'security_question'='$security_question', 'security_answer'='$security_answer', 'credit_confirm'='$credit_confirm' WHERE 'ID'=".$_SESSION['formID'];

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// redirect
header("Location: http://www.xxxx.php");
ob_end_flush();

// close connection
mysql_close($connection);
}
?>

raf
06-19-2006, 08:40 AM
inside your sql-statement, your surround the columnnames with quotes --> need to be removed or replaced by backticks. so ` instead of '.

and i still nowhere see the session_start() so i hope for you that the rest of the code (that you didn't post) is ok...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum