Jump to content
Larry Ullman's Book Forums

Chapter 9 - The User Registration Form


Recommended Posts

Hello

 

I've finished chapter 9 and am currently attempting to create a registration form on the site using HTML_QuickForm2. Everything is working fine, except I can't seem to return error messages if the username and/or email address are already registered. Here is the code after the form has been submitted:

// Add the submit button
if($_SERVER['REQUEST_METHOD']=='POST'){
	
	$errors = array();
	
	// validate the form data
	if($form->validate()){
		//check to see if the email is available
		$select_query = 'SELECT id FROM users WHERE email=:email or username=:username';
		$stmt = $pdo->prepare($select_query);
		$select_result = $stmt->execute(array(':email'=>$email->getValue(), ':username'=>$username->getValue()));
		
		// Check to see if email address is available
		if(mysqli_num_rows($select_result)==0){ // if the email address or username are available
			
			// Insert the user into the database
			$insert_query = 'INSERT INTO users (userType, username, email, pass, dateAdded)
			VALUES (:userType, :username, :email, SHA1(:password), NOW())';
			$stmt = $pdo->prepare($insert_query);
			$insert_result = $stmt->execute(array
			(':userType'=>'public', 
			':username'=>$username->getValue(),
			':email'=>$email->getValue(),
			':password'=>$password->getValue(),
			));
				
			if($insert_result){ // if it ran ok
				// freeze the form upon success
				$form->toggleFrozen(true);
				$form->removeChild($submit);
			} else {
				$errors[] = ('You could not be registered at this time');
			}
		} else { // if the username/password are already registered
			$errors[] = ('The username and/or password have already been registered.');
		} 
	}	
} 

The problem is that only the error message in the 'insert_result' else statement is displayed when I loop through the results in the register.html view. Any other errors added to the array are not displayed, so the user is not notified why they can't register. Is this a problem with the way I'm trying to implement it (objects instead of procedurally) or is there something obvious that I've missed?

 

Hope I've explained myself well and thanks in advance for any suggestions.

 

 

Link to comment
Share on other sites

  • 2 weeks later...
 Share

×
×
  • Create New...