user706499
user706499

Reputation: 49

Make username out of email and check for duplicates

I am trying to take the email, remove everything after @ and then check if there is no duplicates and if there are duplicate add number to username. I can't get right second part. Maybe you can help me to figure it out why its not working.

$username1 = explode( '@', $usermane );
$username=  $username1[0];


   $usersql = "SELECT * FROM wp_users WHERE user_nicename LIKE '$username%'";
   $usercnt = mysqli_num_rows(mysqli_query($con,$usersql));  

// If username username1 username2 etc already exists
    if ($usercnt >= 1) {
        $num = ++$usercnt;             // Increment $usercnt by 1
        $username = $username . $num;  // Add number to username
    }

Upvotes: 1

Views: 74

Answers (1)

PaulS
PaulS

Reputation: 948

You are increasing $usercnt, while you actually want to increase num, to add that to your username. Use strval() to convert an Integer to a String. Try:

if ($usercnt >= 1) {
    $num = $usercnt + 1;             // Num = Usercount + 1
    $username = $username . strval($num);  // Add number to username
}

Upvotes: 1

Related Questions