我希望在新数据库中检查用户名是否符合新用户名。 如果找到用户名,请至少按数字重新生成用户名 或者在名字和姓氏上附加一个漂亮的字符 并再次检查新生成的用户名是否也不存在。
如果它不存在则运行查询INSERT。
我想要这个的原因是因为我不想要重复的用户名 在数据库中。
我试过但它不适合我 请帮忙。
<?php
//check if username already exists
function checkusername($username){
global $dbc_conn, $table_name;
//CHECK IF A USERNAME EXISTS
$query = mysqli_query($dbc_conn,"SELECT COUNT(username) FROM $table_name WHERE username ='{$username}'");
$rows = mysqli_fetch_row($query);
if(count($rows) > 0){
return false;
}else{
return true;
}
}
//This function create new account
function signup($fname, $lname , $regemail , $regpassword , $gender , $school , $day,$month,$year,$school){
global $dbc_conn, $table_name;
//CHECK IF EMAIL ALREADY EXISTS
$query = mysqli_query($dbc_conn,"SELECT Email FROM $table_name WHERE Email ='{$regemail}'");
if (mysqli_num_rows($query) > 0 ){
$error ="<div class='error'><strong> <i class='fa fa-exclamation-triangle'></i> </strong>Sorry, It looks like you already have an account with us.</div>";
}else{
$username = strtolower($fname.$lname);
//Check if username is do not exists, and if it exist
while(checkusername($username)){
//INSERT INTO DATABASE
$query = "INSERT INTO users
(username,FirstName,LastName,Email,
Password,Month,Day,Year,Gender,cell_group)
VALUES(?,?,?,?,?,?,?,?,?,?)";
//PREPARE QUERY
$prepare = $dbc_conn ->prepare($query);
//BIND QUERY
$prepare->bind_param("ssssssisis",$username,$fname,
$lname , $regemail ,
md5($regpassword) , $gender ,
$school , $day,
$month,$year,$school);
//execute requery
if($prepare->execute()){
//SET SESSION
$_SESSION['usigh-ses'] = mysqli_insert_id($dbc_conn);
header("location:gettingstarted.php");
//close connection
mysqli_close($dbc_conn);
exit;
};
}
}
}
?>