Have error here? MYSQL register
#1

I'm trying to do a mysql system work, looking at the wiki and some basic codes, is compiling, but I have a problem:
When I log on the server, the account is not saved in the database !! There is something wrong here?

my DB:
http://i.imgur.com/GII8c57.png
http://i.imgur.com/lTC3ev5.png

Where I use the functions::

public OnPlayerDisconnect(playerid, reason)
pawn Код:
if(Logado[playerid] == 1) {
        SaveStats(playerid);
    }
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
pawn Код:
if(dialogid == DIALOG_REGISTER)
    {
        if(response)
        {
            if(!strlen(inputtext) || strlen(inputtext) > 128)
            {
                new string1[128];
                SendClientMessage(playerid, COLOR_RED, "[ERROR]: You must insert a password between 1-128 characters!");
                format(string1, sizeof(string1), "Welcome %s\nPlease register to contiune.",PlayerName(playerid));
                ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", string1, "Register", "Quit");
            }
            else if(strlen(inputtext) > 0 && strlen(inputtext) < 128)
            {
                new escpass[100];
                mysql_escape_string(inputtext, escpass);
                MySQL_Register(playerid, escpass);
            }
        }
        if(!response)
        {
            ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_MSGBOX, "Kicked - Notice", "You must register to play", "Close", "");
            Kick(playerid);
        }
    }

    if(dialogid == DIALOG_LOGIN)
    {
        if(!response)
        {
            ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_MSGBOX, "Kicked - Notice", "You must login to play", "Close", "");
            Kick(playerid);
        }
        if(response)
        {
            new query[200], rows, fields;
            mysql_format(ConnectSQL, query, sizeof(query), "SELECT `Username` FROM players WHERE Username = '%s' AND Password = SHA1('%e')", PlayerName(playerid), inputtext);
            mysql_query(ConnectSQL, query);
            cache_get_data(rows, fields);

            if(rows) MySQL_Login(playerid);

            if(!rows)
            {
                InvalidLogins[playerid]++;
                if(InvalidLogins[playerid]==4)
                {
                    Kick(playerid);
                }
                else
                {
                    new str1[256];
                    format(str1,sizeof(str1),"{FFFF00}Wrong password Attempt\n\n{00CC00}Dear %s, Enter Your Password:\n{FFFF00}Login chance: (%d/4)",PlayerName(playerid),4-InvalidLogins[playerid]);
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD,"Login Panel",str1,"Login","Quit");
                }
            }

        }
    }
pawn Код:
stock MySQL_Register(playerid, passwordstring[])
{
    new Query[512], IP[16];
    GetPlayerIp(playerid, IP, sizeof(IP));
    strcat(Query,"INSERT INTO `players`(`Username`,`Senha`)");
    strcat(Query,"VALUES ('%s', SHA1('%s'),0,0,0,0,0,0,0,0,0,0,'%d/%d/%d')");
    mysql_format(ConnectSQL,Query,sizeof(Query),Query,PlayerName(playerid),passwordstring);
    mysql_query(ConnectSQL,Query,false);
    Logado[playerid] = 1;
    GivePlayerMoney(playerid, 5000);
    return 1;
}

stock MySQL_Login(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        new Query[512], savestr[50], rows, fields;
        mysql_format(ConnectSQL,Query, sizeof(Query), "SELECT * FROM `players` WHERE `Username` = '%s'", PlayerName(playerid));
        mysql_query(ConnectSQL,Query);
        cache_get_data(rows, fields);
        if(rows)
        {
            cache_get_field_content(0, "money", savestr);           money[playerid] = strval(savestr);
            cache_get_field_content(0, "admin", savestr);           admin[playerid] = strval(savestr);
            cache_get_field_content(0, "VIP", savestr);             VIP[playerid] = strval(savestr);
            cache_get_field_content(0, "deaths", savestr);          deaths[playerid] = strval(savestr);
            Logado[playerid] = 1;
        }
        Logado[playerid] = 1;
    }
    return 1;
}
That part I caught base, do not understand, I find it stop encryptaзгo
can be here 'cause I do not understand this part

pawn Код:
stock MySQL_Register(playerid, passwordstring[])
{
    new Query[512], IP[16];
    GetPlayerIp(playerid, IP, sizeof(IP));
    strcat(Query,"INSERT INTO `players`(`Username`,`Senha`)");
    strcat(Query,"VALUES ('%s', SHA1('%s'),0,0,0,0,0,0,0,0,0,0,'%d/%d/%d')");
    mysql_format(ConnectSQL,Query,sizeof(Query),Query,PlayerName(playerid),passwordstring);
    mysql_query(ConnectSQL,Query,false);
Reply
#2

you need to put SavePlayer under onplayerdisconnect so when a player leaves the server it will save player data into db or just make timer in that time saveplayer data
Reply
#3

EDITED /\
Reply
#4

EDITED /\
Reply
#5

First of all, your table is named "players", not "users".
Second, your INSERT query is too short for such a table, as your table has no default values set for the columns.

Any INSERT query needs every field in the query which doesn't have a default value in the table.



My suggestion:
For any new player, the only required fields (or columns) would be the IP, UserName and password (Senha).
All the others should have a default value of 0.
I guess that would be your column "Predefinido", they're all set to "None".

Your query should be only this:
INSERT INTO Users (IP, Username, Senha) VALUES ('%s', '%s', SHA1('%s'))

Since all other columns would have a default value of 0, MySQL won't need those columns in the query and sets the default values by itself if you don't include them into your query.

In case you want to give some preset money for new players anyway, adding the Money value to your query doesn't mean you have to edit your columns later.
Adding the Money value to your query, you will override the default value set in your table.
Reply
#6

@powerpc
Thank you for the tips man! Now when registering put just to add only the core values it, so will pull least there only saves the rest when the disconnect player

Sorry for my mistake, I changed my database and forgot to change, but is still without saving, very strange!

pawn Код:
stock MySQL_Register(playerid, passwordstring[])
{
    new Query[512], IP[16];
    GetPlayerIp(playerid, IP, sizeof(IP));
    strcat(Query,"INSERT INTO `players`(`Username`,`Senha`)");
    strcat(Query,"VALUES ('%s', SHA1('%s'),0,0,0,0,0,0,0,0,0,0,'%d/%d/%d')");
    mysql_format(ConnectSQL,Query,sizeof(Query),Query,PlayerName(playerid),passwordstring);
    mysql_query(ConnectSQL,Query,false);
That part I got base and edited do not know if it's right!

REP[PowerPC603] += 1;
Reply
#7

@edited again, sorry my english!
Reply
#8

pawn Код:
stock MySQL_Register(playerid, passwordstring[])
{
    new Query[512], IP[16];
    GetPlayerIp(playerid, IP, sizeof(IP));
    mysql_format(ConnectSQL, Query, sizeof(Query), "INSERT INTO `players`(`Username`,`Senha`) VALUES ('%e', SHA1('%e'))",PlayerName(playerid), passwordstring);
    mysql_query(ConnectSQL, Query, false);
This will do.

As you only specified 2 columns (Username and Senha), you only need to supply 2 values as they are linked together.
Specifying 2 columns with 10 values results in a MySQL-error in your query and it won't get executed.

NOTE:
Watch carefully when you use "%s" for saving data inputted by players.
When using mysql_format, better use the %e instead, at least for saving their name.

They could set their name to be something like "john; DROP TABLE Users;" as their name.
This would delete your entire database, you don't want that.
The %e automatically escapes their name, so you don't become a victim of SQL-injection.

The password also needs to be escaped, as it may contain SQL code as well when you take it directly from the input-field of a dialog.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)