How to prevent duplicate usernames when people register? PHP/MySQL

怎甘沉沦 提交于 2019-11-30 20:25:20

You can do it like this when the user post the username for example and click submit you can write this code or add it to your code with your modification :

<?php
$connect = mysql_connect('whatever','whatever','whatever');
$database = mysql_select_db('your dbname');
$username = $_POST['username'];
if(isset($username)){
$mysql_get_users = mysql_query("SELECT * FROM table_name where username='$username'");

$get_rows = mysql_affected_rows($connect);

if($get_rows >=1){
echo "user exists";
die();
}

else{
echo "user do not exists";
}



}
?>

I think something like this is what you're looking for:

if (isset($_POST['user']))
{
    $user = $_POST['user'];

    if (mysql_num_rows(yourFunctionForQueryingMySQL("SELECT * FROM tablename WHERE user='$user'")))
            echo "Name is taken";
    else echo "Name available";
}

There are two things you should do.

  1. Make the user name a primary key in the database table. This is easily done using phpmyadmin.

  2. Validate prior to insert.

For the second step, here's an algorithm. You may implement it in your own way using pdo, mysqli or even mysql (although it's not recommended now).

Algorithm at the end of your code (i.e., if there aren't errors)...

Select records that match the USERNAME supplied in the post.

If it exists, give out an error.

If it doesn't exist, insert it.

Hope that helps.

I used a PDO and class method, may be of some use.

//function for checking if the user already exists in the database
 public function userExists($username){

//prepared statements for added security
$query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username`= ?");
$query->bindValue(1, $username);

try{
    //execute the query
    $query->execute();
    $rows = $query->fetchColumn();

    //if a row is returned...user already exists
    if($rows == 1){
        return true;
    }else{
        return false;
    }
//catch the exception
}catch (PDOException $e){
    die($e->getMessage());
}
 }//end function userExists

note the prepared statements too - definitely the way forward

you can do like this :

<?php

 //---put this code before "if (empty($error))"------

 $username = $_POST['username'];
 //---if your table user is "table_user" and fieldname username is "username"
 $query = 'SELECT username FROM table_user WHERE username = "' . $username . '" LIMIT 1';
 $result  = mysql_query($query)
 $totalNumRowResult = mysql_num_rows($result);

 if($totalNumRowResult > 0) {
  $error[] = 'User exist';
 }

?>

Hope that helps.

You have to (can) make condition that forbids registration if username already exists.

    $sql="SELECT username FROM tablename WHERE username='".$username."'";//looking through existing usernames
    $resul=mysql_query($sql);
    $num = mysql_num_rows($resul);

    if ($num !=0){ //If there is already such username...

    die("There is user with that username.");  // ...kill the script!
} else //continue with the script
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!