Solved

PHP Registration  Form not submitting

Posted on 2011-09-15
5
215 Views
Last Modified: 2012-08-14
I'm trying to setup a user registration form but when I submit the form this error displays...Please re-enter your passwords and try again.

Any ideas why?  Here's the code...



<?php if (isset($_POST['submitted'])) { // Handle the form.

      require_once (MYSQL);
      
      // Trim all the incoming data:
      $trimmed = array_map('trim', $_POST);
      
      // Assume invalid values:
      $fn = $ln = $e = $p = FALSE;
      
      // Check for a first name:
      if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) {
            $fn = mysqli_real_escape_string ($dbc, $trimmed['first_name']);
      } else {
            echo '<p class="error">Please enter your first name!</p>';
      }
      
      // Check for a last name:
      if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) {
            $ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']);
      } else {
            echo '<p class="error">Please enter your last name!</p>';
      }
      
      // Check for an email address:
      if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
            $e = mysqli_real_escape_string ($dbc, $trimmed['email']);
      } else {
            echo '<p class="error">Please enter a valid email address!</p>';
      }

      // Check for a password and match against the confirmed password:
      if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) {
            if ($trimmed['password1'] == $trimmed['password2']) {
                  $p = mysqli_real_escape_string ($dbc, $trimmed['password1']);
            } else {
                  echo '<p class="error">Your password did not match the confirmed password!</p>';
            }
      } else {
            echo '<p class="error">Please enter a valid password!</p>';
      }
      
      if ($fn && $ln && $e && $p) { // If everything's OK...

            // Make sure the email address is available:
            $q = "SELECT user_id FROM users WHERE email='$e'";
            $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
            
            if (mysqli_num_rows($r) == 0) { // Available.
            
                  // Create the activation code:
                  $a = md5(uniqid(rand(), true));
            
                  // Add the user to the database:
                  $q = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() )";
                  $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

                  if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
                  
                        // Send the email:
                        $body = "Thank you for registering at <whatever site>. To activate your account, please click on this link:\n\n";
                        $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
                        mail($trimmed['email'], 'Registration Confirmation', $body, 'From: admin@sitename.com');
                        
                        // Finish the page:
                        echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';
                        include ('includes/footer.php'); // Include the HTML footer.
                        exit(); // Stop the page.
                        
                  } else { // If it did not run OK.
                        echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
                  }
                  
            } else { // The email address is not available.
                  echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>';
            }
            
      } else { // If one of the data tests failed.
            echo '<p class="error">Please re-enter your passwords and try again.</p>';
      }

      mysqli_close($dbc);

} // End of the main Submit conditional.
?>
      
      
0
Comment
Question by:BrighteyesDesign
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I may not be able to debug your code for you but I can show you how it is done.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

Going forward, please use the code snippet to post the code.  If you do that we get a unispace font and line numbers.  It is much easier to read and discuss code when we have those things.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I believe that this is the salient part of the code that would cause the message to appear.  
if ($fn && $ln && $e && $p) { // If everything's OK..                        
      } else { // If one of the data tests failed.
            echo '<p class="error">Please re-enter your passwords and try again.</p>';
      }

Open in new window

You might want to use var_dump() to print out the contents of the variables that are used in the if() statement.  You might want to add error_reporting(E_ALL) to the top of the script so that you can be sure you are not accidentally relying on an undefined variable or constant.
0
 

Author Comment

by:BrighteyesDesign
Comment Utility
Thanks Ray,

I'm working though a PHP book by Larry Ullman - PHP 6 and MySQL 5 for Dynamic Web Sites.

This script works with others (activate,lost password etc...) so i'd like to stick with it if possible.

I have reposted the code as you suggested so hopefully an errors will be easier to spot!


<?php if (isset($_POST['submitted'])) { // Handle the form.

      require_once (MYSQL);
      
      // Trim all the incoming data:
      $trimmed = array_map('trim', $_POST);
      
      // Assume invalid values:
      $fn = $ln = $e = $p = FALSE;
      
      // Check for a first name:
      if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) {
            $fn = mysqli_real_escape_string ($dbc, $trimmed['first_name']);
      } else {
            echo '<p class="error">Please enter your first name!</p>';
      }
      
      // Check for a last name:
      if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) {
            $ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']);
      } else {
            echo '<p class="error">Please enter your last name!</p>';
      }
      
      // Check for an email address:
      if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
            $e = mysqli_real_escape_string ($dbc, $trimmed['email']);
      } else {
            echo '<p class="error">Please enter a valid email address!</p>';
      }

      // Check for a password and match against the confirmed password:
      if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) {
            if ($trimmed['password1'] == $trimmed['password2']) {
                  $p = mysqli_real_escape_string ($dbc, $trimmed['password1']);
            } else {
                  echo '<p class="error">Your password did not match the confirmed password!</p>';
            }
      } else {
            echo '<p class="error">Please enter a valid password!</p>';
      }
      
      if ($fn && $ln && $e && $p) { // If everything's OK...

            // Make sure the email address is available:
            $q = "SELECT user_id FROM users WHERE email='$e'";
            $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
            
            if (mysqli_num_rows($r) == 0) { // Available.
            
                  // Create the activation code:
                  $a = md5(uniqid(rand(), true));
            
                  // Add the user to the database:
                  $q = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() )";
                  $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

                  if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
                  
                        // Send the email:
                        $body = "Thank you for registering at <whatever site>. To activate your account, please click on this link:\n\n";
                        $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
                        mail($trimmed['email'], 'Registration Confirmation', $body, 'From: admin@sitename.com');
                        
                        // Finish the page:
                        echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';
                        include ('includes/footer.php'); // Include the HTML footer.
                        exit(); // Stop the page.
                        
                  } else { // If it did not run OK.
                        echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
                  }
                  
            } else { // The email address is not available.
                  echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>';
            }
            
      } else { // If one of the data tests failed.
            echo '<p class="error">Please re-enter your passwords and try again.</p>';
      }

      mysqli_close($dbc);

} // End of the main Submit conditional.
?>

Open in new window

0
 

Author Comment

by:BrighteyesDesign
Comment Utility
Thanks Ray, i'll try that...
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
I think the if() on line 43 lines up with the else on line 78.  So it appears that the else runs and causes the message.  Since this is data dependent, you might want to print out the data with var_dump(), using something like this at line 42, right before the if() statement.
$signal_array = array
( 'fn' => $fn
, 'ln' => $ln
, 'e'  => $e
, 'p'  => $p
)
;
var_dump($signal_array);

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now