当一个用户名被插入到我的数据库中时,比如:
- John_Smith
我需要检查是否已经有一个叫John_Smith的存在。如果是的话,将其增加1变为John_Smith_1。
所以,如果以下用户名已经存在:
- John_Smith
- John_Smith_1
- John_Smith_2
- John_Smith_3
- ....一直到John_Smith_10
我需要下一个插入的John_Smith被递增为John_Smith_11。
到目前为止,我已经搜索并找到了这个:
$preferredname= "John_Smith"
//check for duplicate user names
$duplicate= check_for_duplicate_username($preferredname);
//if duplicate, increment the preferredname
if ($duplicate)
{
$parts = explode("_",$preferredname);
if (isset($parts[1]))
$preferredname = $parts[0]."_".$parts[1]."_".($parts[2]+1);
else $preferredname = $parts[0]."_".$parts[1]."_1";
}
我相信这只能适用于第一个匹配的用户名。我的问题是检查数据库中版本号最高的名称。这是我的SQL语句:
function check_for_duplicate_username($name)
{
// check if username already exists
$sql = "SELECT * FROM users WHERE user_username=$name";
//then return duplicates