0

I am attempting to write a method that checks whether a user exists and also does some validation.

This is the code so far:

public function checkUsername(){
            if((strlen($_POST['register-username']) > 2) && (strlen($_POST['register-username']) < 16)){
                $stmt = $this->dbh->prepare("SELECT username FROM adrenaline_junkies_uk_users WHERE username = ?");
                $stmt->bindParam(1, $this->post_data['register_username'], PDO::PARAM_STR);
                $stmt->execute();
                if($stmt->rowCount() != 0){
                        return TRUE;
                }else{
                    return $this->error .= '<span style="color:red;">Username already exists.</span>';
                }
            }else{
                return $this->error .= '<span style="color:red;">Username must be between 3 and 15 characters.</span>';
            }

    }

This is how Im attempting to call it:

if( isset($_POST['register-submit'])){

        $error = '';
        $register = new register($_POST, $dbh);
            if(!$error .= $register->checkUsername()){

                    //continue

            }else{
                $error .= $register->checkUsername();
            }
    }

To test it I don't enter anything in the input field to get the first error to be returned and echo it out correctly on the webpage. But nothing is displaying.

Is this the correct way of doing this? Sorry I'm not very familiar with using methods in classes. I assume I'm doing something wrong in the initial if statement in the calling program and should I be running that method twice like I do?

1 Answer 1

1

Use Exceptions. Like:

public function checkUsername(){
    if((strlen($_POST['register-username']) > 2) && (strlen($_POST['register-username']) < 16)){
        $stmt = $this->dbh->prepare("SELECT username FROM adrenaline_junkies_uk_users WHERE username = ?");
        $stmt->bindParam(1, $this->post_data['register_username'], PDO::PARAM_STR);
        $stmt->execute();
        if($stmt->rowCount() != 0){
                return TRUE;
        }else{
            throw new Exception("Username already exists.");
        }
    }else{
        throw new Exception("Username must be between 3 and 15 characters.");
    }

}

and

if( isset($_POST['register-submit'])){

    $error = '';
    $register = new register($_POST, $dbh);
    try {
        if($register->checkUsername()){
            //continue
        }
    } catch ($e) {
        $error .= '<span style="color:red;">' . $e->getMessage() . '</span>';
    }
}

You can do subclassing like:

class UsernameException extends Exception {}

try {
    throw new UsernameException("Your username is too awesome");
} catch (UsernameException $e) {
    exit($e->getMessage());
} catch (SomeOtherException $e) {
    exit("500");
} catch (Exception $e) {
    exit("que?");
}
Sign up to request clarification or add additional context in comments.

6 Comments

I get this error attempting your code: Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING
Well, I probably made a typo somewhere. Maybe it's } catch ($e) {, change to } catch (Exception $e) {
Just wondering if i follow the checkUsername() method with one that checks password, would it catch any returned string from that and any other returned strings?
My implementation doesn't use the return value of checkUsername. I expect it has no return value (it just throws Exceptions).
Well in my example it has the return value of that string, I thought perhaps the exception was catching anything that isn't true? What exactly is the exception doing?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.