senso Posted August 31, 2013 Share Posted August 31, 2013 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 More sharing options...
Larry Posted September 5, 2013 Share Posted September 5, 2013 Sorry for the delay. Was out of town last weekend. Are you still having issues with this? Link to comment Share on other sites More sharing options...
senso Posted September 6, 2013 Author Share Posted September 6, 2013 Hi Larry Thanks for the reply Yes, I'm still having issues, but I'm guessing I need to use try - catch statements instead of using an array of errors, like you demo'd in the book? Link to comment Share on other sites More sharing options...
Larry Posted September 17, 2013 Share Posted September 17, 2013 What's with the parentheses around the error values? Those should just be strings. Link to comment Share on other sites More sharing options...
Recommended Posts