therecluse26 / PHP-Login

A login system built with PHP, MySQL, jQuery and Bootstrap
MIT License
839 stars 444 forks source link

Duplicate Username #216

Closed rohit1290 closed 5 years ago

rohit1290 commented 6 years ago

While user registration if the username already exists then the error is shown as Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin' for key 'username_UNIQUE'

This should be simplified as "Username not available"

budiony commented 5 years ago

The problem can be solved easy by placing the following code in file userhandler.php, around line 32:

//Check if username or email exist upon registration
          $exists = $db->conn->prepare("SELECT username, email FROM ".$db->tbl_members. " WHERE username = :username OR email = :email LIMIT 1");
foreach ($userarr as $user) {
            $exists->bindParam(':username', $user['username']);
            $exists->bindParam(':email', $user['email']); 
            $exists->execute();
            $res = $exists->fetch(\PDO::FETCH_ASSOC);
          }          
          //Member with such email does not exists, proceed with registration
          if (empty($res)) {
          //The rest of the code goes here
           }