我想根据他/她的名字为每个用户生成唯一的用户名,用户可以稍后更改。如果用户在注册表中输入'smith doe'作为他的名字,则自动生成的用户名应为'smith',但如果'smith'已经是某人的用户名(u_name),那么它应该在'smith'末尾添加任何可用的数字:喜欢''smith1'或'smith2'或'smith3'等......
以下是我的尝试:
$usnm = $_POST['name'];
$first_nut = explode(' ', $usnm);
$usnm3 = $first_nut[0];
$usnm5 = $usnm3;
function generateUsername($usnm3,$iteration = 0)
{
$generated = $iteration > 0 ? ($usnm3 . $iteration) : $usnm3; //Increment username
$query_usnm = mysqli_query($con, "SELECT * FROM user WHERE (u_name = '$generated' or u_name = '$usnm3') ");
if(mysqli_num_rows($query_usnm) > 0)
{
return generateUsername($usnm3,$iteration + 1);
}
return $generated;
}
$usnm4 = generateUsername($usnm5);
$sql = mysqli_query($con, "insert into user (name, u_name) values ('$name', '$usnm4');");