Jump to content

Function to check if customer Username exists in another account


vline

Recommended Posts

I'm adapting Prestashop to eFront and have created a Username variable that gets passed onto the LMS using an API script.  I'm currently trying to create a new function to check the database to see if anyone else is using the same username but I can't seem to get it to work.  In the Customer.php file in Classes, I've created the function below:

 

static public function usernameExists($username)
    {
         if (!Validate::isGenericName($username))
             die (Tools::displayError());

        $result = Db::getInstance()->getRow('
        SELECT `id_customer`
        FROM `'._DB_PREFIX_.'customer`
        WHERE `username` = \''.pSQL($username).'\'');
        return isset($result['id_customer']);
    }

 

In the authentication.php file, I added the line:

 

elseif (Customer::usernameExists($username))
                $errors[] = Tools::displayError('Someone has already registered with this username');  

 

beneath the one that checks to see if the same e-mail is being used by another user.  It seems to always pull up the 01. Error, "Someone has already registered with this username" even when I've created a username I know isn't being used and it won't create the account.

 

Any help would be greatly appreciated.  I'm stumped and have been working all day on this issue.

Link to comment
Share on other sites

What version of Prestashop are we talking about?

 

I would suggest checking if $result is true or false, and not to use isset.  That is because getRow will return false if the row is not found, or an array if the row is found.  Since you are not returning the array or the id_customer from this function, there is no reason to even look at the array

 

Something like this instead

static public function usernameExists($username)
    {
         if (!Validate::isGenericName($username))
             die (Tools::displayError());

        $result = Db::getInstance()->getRow('
        SELECT `id_customer`
        FROM `'._DB_PREFIX_.'customer`
        WHERE `username` = \''.pSQL($username).'\'');

        if ($result)
            return true;
        else
            return false;
    }
Link to comment
Share on other sites

Thank you, bellini13!  I'll try it out.  Honestly, I'm not sure what version it is.  It's pretty old but the changes I've made to it are so extensive that updating it is no longer possible without undoing a lot of work.  I'll let you know how that works.  Thanks again.

Link to comment
Share on other sites

It doesn't work.  The script keeps coming back as a positive match that the username is being used even though there isn't another account using that username.  It keeps stopping the registration process and showing that there is an error that the username already exists.  Any other thoughts?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...