/changename already exist name help
#1

PHP Code:
CMD:changename(playeridparams[]) {
    if(
isnull(params)) {
        return 
SendClientMessage(playerid, -1"/changename [newname]");
    }
    if(
strlen(params) > MAX_PLAYER_NAME) {
        return 
SendClientMessage(playerid, -1"Your name must contain a maximum of 24 characters.");
    }
    new 
current[MAX_PLAYER_NAME];
    
GetPlayerName(playeridcurrentsizeof current);
    if(!
strcmp(currentparamsfalse)) {
        return 
SendClientMessage(playerid, -1"That's already your name!");
    }
    new 
query[128];
    
mysql_format(handlequerysizeof query"SELECT * FROM `users` WHERE `name` = '%s'"params);
    
mysql_tquery(handlequery"ChangeName""is"playeridparams);
    return 
1;
}
forward ChangeName(playeridnewname[]);
public 
ChangeName(playeridnewname[]) {
    new 
current[MAX_PLAYER_NAME];
    
GetPlayerName(playeridcurrentsizeof current);
    new 
query[128];
    
mysql_format(handlequerysizeof query"UPDATE `users` SET `name` = '%s' WHERE `name` = '%s'"newnamecurrent);
    
mysql_tquery(handlequery);
    
SetPlayerName(playeridnewname);
    return 
1;

How to make detect Name Already Exist, and player must try more name?
Reply
#2

Code:
 if(cache_num_rows()) return error;
Reply
#3

ALWAYS escape input by people to avoid SQL Injection.

Set `name` column as UNIQUE KEY, not only the searching will be faster but it will not continue searching if a row is found because it now knows there will never be duplicates.

In /changename:
pawn Code:
mysql_format(..., "UPDATE IGNORE `users` SET `name` = '%e' WHERE `name` = '%s'", ...);
mysql_tquery(handle, query, "OnPlayerNameChange", "ds", playerid, params);
In `OnPlayerNameChange` callback:
pawn Code:
// without `IGNORE` keyword, it would return error about duplicate key
// `IGNORE` literally ignores the error and does absolutely nothing (0 rows are affected)
// When the row is updated (sets the new name), the affected rows will be 1
if (!cache_affected_rows())
{
    // already registered name, pick a new one
}
else
{
    // SetPlayerName
}
Reply
#4

Quote:
Originally Posted by SiaReyes
View Post
Code:
 if(cache_num_rows()) return error;
thanks for reference
Reply
#5

Fore more secure and faster method, I suggest you to use Calisthenics's method.
Reply
#6

Will it works like this?

Quote:

if(cache_num_rows() > 0)
{
return SCM blah blah blah "Someone else is using this name!");
}

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)