Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts

    2 page registration form

    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

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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/functio...-insert-id.php)
    you then store that id in a sessionvariable. like:
    PHP Code:
    $_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:
    PHP Code:
    $sql "UPDATE yourtable SET foo=bar WHERE yourIDcolumn="$_SESSION['key_formID']; 
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:

    Code:
    $_SESSION['ID'] = mysql_insert_id();
    and

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

  • #6
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    No ...
    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...

    PHP Code:
    $_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?
    Mike

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    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:

    PHP Code:
    $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.
    Mike

  • #9
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Anarchy
    Here's the code I currently have on page 1:

    PHP Code:
    <?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?

    PHP Code:
    $_SESSION['formID'] = mysql_insert_id(); 
    Then on page two of the registration from do as follows:

    PHP Code:
    <?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); 

    ?>

  • #10
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    I would put it after you run the insert so...
    PHP Code:
        $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.
    Mike

  • #11
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

  • #12
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    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?
    Mike

  • #13
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #14
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    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!

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

  • #15
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    d'oh!
    I got this error

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

    Code:
    $sql = "UPDATE client SET `maiden`='$maiden', `security_question`='$security_question' WHERE `ID`="._SESSION['formID'];
    Last edited by losse; 06-16-2006 at 02:30 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •