This topic is locked

Prevent duplicate usernames

6/4/2014 11:00:58 PM
PHPRunner Tips and Tricks
S
Stucco author

Hi, I had to accomplish this recently and wanted to share with the community.
I was asked to prevent duplicate usernames from being added to the database. It isn't too difficult.
I added the following to the Events -> admin_users -> Add page -> Before record added



$count = DBLookup("select count(id) from users where username=\"".$values['username']."\"");

if($count==1){

$message="Username '".$values['username']."' already exists. Please choose another.";

$values['password']="";

}

return $count==0;


This takes care of it!
Thanks for all the help.

Sergey Kornilov admin 6/12/2014

Just wanted to add that this approach to prone to SQL injection attacks. The use of built-in 'Prevent duplicate values' functionality is more reliable.