• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Multiple-page signup form doesn't fully write to SQL database

I have a sign up form split across 3 web pages (signup1.php, signup2.php, and soon-to-be-built signup3.php). Signup1.php functions fine, and captures the data into my SQL database. With Signup2.php, I want it to add data into the same database row where email matches the email submitted in the first signup form. Basically, I want signup1.php to gather some data (and write it to SQL) and then signup2.php to capture a little more data (and write to same SQL row), and then do the same with a signup3 form (not built yet). However, I just can't get the signup2 to function properly.

Form from Signup1.php (works fine!):

<form class="form" action="include/process_register1.php" method="POST">
            <label>
               <span>First name</span>
               <input type="text" class="input_text" name="fname" id="fname" size="40"/>
            </label>
            <label>
               <span>Last name</span>
               <input type="text" class="input_text" name="lname" id="lname" size="40"/>
            </label>
             <label>
               <span>Email</span>
               <input type="text" class="input_text" name="email" id="email" size="40"/>
            </label>
            <label>
               <span>Zipcode</span>
               <input type="text" class="input_text" name="zipcode" id="email" maxlength="5"/>
            </label>
             <input type="submit" class="button" value="Next" />      
                          </form>


SQL for Signup1.php (works fine!):

function insertUser1($fname, $lname, $email, $zipcode) {

      $myQuery = "INSERT INTO musicmatch_users (fname, lname, email, zipcode) VALUES ('{$fname}', '{$lname}', '{$email}', '{$zipcode}')";
      
      //run the query
      $result = mysql_query($myQuery);
}




Form from Signup2.php (Doesn't work!):

<form class="form" action="include/process_register2.php" method="POST">
            <label>Main Instrument<a href="#"><img src="images/labelquestion.png" width="25" height="25"></a></label>
            <select name="instrument1" data-placeholder="" class="chzn-select">
              <option value="Electric Bass">Electric Bass </option>
              <option value="Acoustic Bass">Acoustic Bass </option>
              <option value="Drum">Drum Kit </option>
              <option value="Acoustic Guitar">Acoustic Guitar </option>
              <option value="Acoustic-Electric Guitar">Acoustic-Electric Guitar </option>
              <option value="Banjo">Banjo </option>
              <option value="Bassoon">Bassoon </option>
              <option value="Cellos">Cellos </option>
              <option value="Clarinet">Clarinet </option>
              <option value="Double Bass">Double Bass </option>
              <option value="Doumbek">Doumbek </option>
              <option value="Electric Guitar">Electric Guitar </option>
              <option value="Electronic Drums">Electronic Drums </option>
              <option value="Other Percussion">Other Percussion</option>
              <option value="Flute">Flute </option>
              <option value="French horn">French horn </option>
              <option value="Fretless Acoustic Bass">Fretless Acoustic Bass </option>
              <option value="Fretless Electric Bass">Fretless Electric Bass </option>
              <option value="Fretted Acoustic Bass">Fretted Acoustic Bass </option>
              <option value="Keyboard">Keyboard </option>
              <option value="Saxaphone">Saxaphone </option>
              <option value="Synthesizer">Synthesizer </option>
              <option value="Trombone">Trombone </option>
              <option value="Trumpet">Trumpet </option>
              <option value="Ukulele">Ukulele </option>
              <option value="Upright Bass">Upright Bass </option>
              <option value="Violin">Violin</option>
            </select>
   <label>Other Instrument<a href="#"><img src="images/labelquestion.png" width="25" height="25"></a> </label>
            <select name="instrument2" data-placeholder="" class="chzn-select">
              <option value="Electric Bass">Electric Bass </option>
              <option value="Acoustic Bass">Acoustic Bass </option>
              <option value="Drum">Drum Kit </option>
              <option value="Acoustic Guitar">Acoustic Guitar </option>
              <option value="Acoustic-Electric Guitar">Acoustic-Electric Guitar </option>
              <option value="Banjo">Banjo </option>
              <option value="Bassoon">Bassoon </option>
              <option value="Cellos">Cellos </option>
              <option value="Clarinet">Clarinet </option>
              <option value="Double Bass">Double Bass </option>
              <option value="Doumbek">Doumbek </option>
              <option value="Electric Guitar">Electric Guitar </option>
              <option value="Electronic Drums">Electronic Drums </option>
              <option value="Other Percussion">Other Percussion</option>
              <option value="Flute">Flute </option>
              <option value="French horn">French horn </option>
              <option value="Fretless Acoustic Bass">Fretless Acoustic Bass </option>
              <option value="Fretless Electric Bass">Fretless Electric Bass </option>
              <option value="Fretted Acoustic Bass">Fretted Acoustic Bass </option>
              <option value="Keyboard">Keyboard </option>
              <option value="Saxaphone">Saxaphone </option>
              <option value="Synthesizer">Synthesizer </option>
              <option value="Trombone">Trombone </option>
              <option value="Trumpet">Trumpet </option>
              <option value="Ukulele">Ukulele </option>
              <option value="Upright Bass">Upright Bass </option>
              <option value="Violin">Violin</option>
            </select>
              <label>Years of Experience<a href="#"><img src="images/labelquestion.png" width="25" height="25"></a></label>
            <select name="experience" data-placeholder="" class="chzn-select">
              <option value="0-1">Less than 1 year</option>
              <option value="1-3">1 to 3 years</option>
              <option value="3-5">3 to 5 years</option>
              <option value="5-10">5 to 10 years</option>
              <option value="10+">More than 10 years</option>
            </select>
            <label>First Favorite Artist<a href="#"><img src="images/labelquestion.png" width="25" height="25"></a></label>
            <?php include('include/forms/artist1.php'); ?>
            <label>Second Favorite Artist<a href="#"><img src="images/labelquestion.png" width="25" height="25"></a></label>
            <?php include('include/forms/artist2.php'); ?>
            <label>First Favorite Music Genre<a href="#"><img src="images/labelquestion.png" width="25" height="25"></a></label>
            <?php include('include/forms/genre1.php'); ?>
            <label>Second Favorite Music Genre<a href="#"><img src="images/labelquestion.png" width="25" height="25"></a></label>
            <?php include('include/forms/genre2.php'); ?>
           
            <br class="clear"/>
            <label> </label>
            <input type="submit" class="button" value="Next" />
            <script type="text/javascript"> $(".chzn-select").chosen(); $(".chzn-select-deselect").chosen({allow_single_deselect:true}); </script>
          </form>



SQL for Signup2.php (Doesn't work!):

function insertUser2($instrument1, $instrument2, $experience, $genre1, $genre2, $artist1, $artist2) {
      $myQuery = "INSERT INTO musicmatch_users (instrument1, instrument2, experience, genre1, genre2, artist1, artist2) VALUES ('{$instrument1}','{$instrument2}','{$experience}','{$genre1}','{$genre2}','{$artist1}','{$artist2}') WHERE email = '$email'";
      
      //run the query
      $result = mysql_query($myQuery);
}


Thank you for your time!

Best,

Chris
signup1.php
signup2.php
process-register1.php
process-register2.php
functions.php
session.php
process-session.php
0
chris_alex
Asked:
chris_alex
  • 2
1 Solution
 
Ray PaseurCommented:
Rather than try to delve into all of the information posted here, I will just try to suggest a design pattern.

When the first signup step is completed, get the key of the inserted row with something like mysql_insert_id().
http://php.net/manual/en/function.mysql-insert-id.php

Store that value in the session and use it for the key for the queries in the subsequent signup pages.
0
 
chris_alexAuthor Commented:
@Ray_Paseur Thanks for the suggestion. That definitely sounds like the way to go. Could you help me out with the exact implementation? Would it be something like this?

function insertUser1($fname, $lname, $email, $zipcode) {
      $myQuery = "INSERT INTO musicmatch_users (fname, lname, email, zipcode) VALUES ('{$fname}', '{$lname}', '{$email}', '{$zipcode}')";
            //run the query
      $result = mysql_query($myQuery);
      $id = mysql_insert_id();
}

...then...

<?php
//start PHP session
  session_start();
//create a session variable
$_SESSION['loggedin']  = true;
$_SESSION['email']  = $email;
$_SESSION['fname']  = $fname;
$_SESSION['id']  = $id;
?>

...then for the second sign up page...

function insertUser2($instrument1, $instrument2, $experience, $genre1, $genre2, $artist1, $artist2) {
      $myQuery = "INSERT INTO musicmatch_users (instrument1, instrument2, experience, genre1, genre2, artist1, artist2) VALUES ('{$instrument1}','{$instrument2}','{$experience}','{$genre1}','{$genre2}','{$artist1}','{$artist2}') WHERE id = '$id'";
            //run the query
      $result = mysql_query($myQuery);
}
0
 
Ray PaseurCommented:
You're fairly close there.  Put session_start() at the top of all scripts, unconditionally, all of the time.  You will need to use some error checking on your queries.  The code snippet shows how to do that.  And instead of this...

 $id = mysql_insert_id();

Do this...

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

Then use the session variable in your query.

<?php // RAY_mysql_example.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);


// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQL
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// MAN PAGE: http://php.net/manual/en/function.mysql-query.php
// MAN PAGE: http://php.net/manual/en/function.mysql-errno.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
// MAN PAGE: http://php.net/manual/en/function.mysql-num-rows.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-array.php
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php
// MAN PAGE: http://php.net/manual/en/function.error-log.php


// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $err <br/>";
}

// SELECT THE MYSQL DATA BASE
if (!mysql_select_db($db_name, $db_connection))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $err <br/>";
    die('NO DATA BASE');
}
// IF THE SCRIPT GETS THIS FAR IT CAN DO QUERIES


// ESCAPE ALL DATA FIELDS BEFORE USE IN MYSQL QUERIES
$safe_username = mysql_real_escape_string($_POST["username"]);


// CREATE AND SEND A SELECT QUERY AND TEST THE RESULTS
$sql = "SELECT id FROM my_table WHERE username='$safe_username'";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = "QUERY FAIL: "
    . $sql
    . ' ERRNO: '
    . mysql_errno()
    . ' ERROR: '
    . mysql_error()
    ;
    error_log($err);

    // HANDLE THE PROGRAMMATIC CONSEQUENCES HERE
    die($err);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS


// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num = mysql_num_rows($res);
$fmt = number_format($num);
if (!$num)
{
    echo "<br/>QUERY FOUND NO DATA: ";
    echo "<br/> $sql <br/>";
}
else
{
    echo "<br/>QUERY FOUND $fmt ROWS OF DATA ";
    echo "<br/> $sql <br/>";
}


// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row = mysql_fetch_assoc($res))
{
    // ROW BY ROW PROCESSING IS DONE HERE
    var_dump($row);
}


// ANOTHER WAY OF DETERMINING HOW MANY ROWS WE HAVE IN A TABLE
$sql = "SELECT COUNT(*) FROM my_table";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($err);
}
// GET THE RESULTS SET ROW IN AN ARRAY WITH A NUMERIC INDEX - POSITION ZERO IS THE COUNT
$row = mysql_fetch_array($res, MYSQL_NUM);
$num = $row[0];
$fmt = number_format($num);
echo "<br/>THERE ARE $fmt ROWS IN THE TABLE";


// MAKING AN INSERT QUERY AND TESTING THE RESULTS
$sql = "INSERT INTO my_table (username) VALUES ('$safe_username')";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/> $sql <br/>";
    die($err);
}


// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED - PER THE DB CONNECTION
$id  = mysql_insert_id($db_connection);
echo "<br/>YOU JUST INSERTED A RECORD WITH AUTO_INCREMENT ID = $id";

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now