SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 29/03/2020, 01:05 AM   #1
brianrestup
Little Clucker
 
Join Date: Sep 2019
Location: Indonesia
Posts: 18
Reputation: 0
Question /changename already exist name help

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?
brianrestup is offline   Reply With Quote
Old 29/03/2020, 03:24 AM   #2
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 618
Reputation: 57
Default Re: /changename already exist name help

Code:
 if(cache_num_rows()) return error;
SiaReyes is offline   Reply With Quote
Old 29/03/2020, 08:59 AM   #3
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,227
Reputation: 205
Default Re: /changename already exist name help

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
}
__________________
Calisthenics is offline   Reply With Quote
Old 29/03/2020, 10:47 AM   #4
brianrestup
Little Clucker
 
Join Date: Sep 2019
Location: Indonesia
Posts: 18
Reputation: 0
Default Re: /changename already exist name help

Quote:
Originally Posted by SiaReyes View Post
Code:
 if(cache_num_rows()) return error;
thanks for reference
__________________
  • Kick(playerid);
  • Ban(playerid);
brianrestup is offline   Reply With Quote
Old 29/03/2020, 11:07 AM   #5
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 618
Reputation: 57
Default Re: /changename already exist name help

Fore more secure and faster method, I suggest you to use Calisthenics's method.
SiaReyes is offline   Reply With Quote
Old 29/03/2020, 02:45 PM   #6
MichiCZ
Big Clucker
 
Join Date: Dec 2011
Location: Czech Republic - Prachatice/Písek
Posts: 109
Reputation: 5
Default Re: /changename already exist name help

Will it works like this?

Quote:
if(cache_num_rows() > 0)
{
return SCM blah blah blah "Someone else is using this name!");
}
MichiCZ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Ajuda] If DynamicPickup EXIST && Dynamic3DTextLabel EXIST binnyl Português/Portuguese 2 28/01/2018 09:23 PM
Does it exist Micko123 Scripting Help 5 13/11/2016 04:32 PM
Already Exist StR_MaRy Scripting Help 2 13/04/2016 01:57 PM
Command doesnt exist but it exist at my gamemode Trynda Scripting Help 24 02/05/2014 10:36 AM
txd exist. Roel Scripting Help 2 07/10/2012 04:59 PM


All times are GMT. The time now is 03:38 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.