MySQL Account Problem
#1

Whenever I register an account, the server registers an account.
As soon as I disconnect and reconnect without a server restart, the server shows that I am a registered player.
However, if I restart the server in between the disconnection and reconnection the server doesn't recognize my account, and registers another one.
I posted the functions that may influence this.
If I forgot something, I will add it.

PasteBin Link

Thank you in advance.
Reply
#2

what mysql plugin are you using ?
Reply
#3

G-Stylez
Reply
#4

This Should work..didnt tested it...

pawn Код:
#include <a_samp>
#include <a_mysql>

#define MYSQL_HOST "localhost" // change this if its needed
#define MYSQL_USER "User" // Database username to connect
#define MYSQL_PASS "My Mysql Password" // The MySQL Password for connection
#define MYSQL_DB "My Database" // Database name

#define DIALOG_REGISTER 7000
#define DIALOG_LOGIN 7000

#define COLOR_BROWN 0x8A6A0BAA
#define COLOR_ERROR 0xFF0000AA
#define COLOR_GREEN 0x00FF00AA

#pragma tabsize 0 // Tells the loose indention to shut up :)

new query[160]; // Will be needed for later use
new LoggedIn[MAX_PLAYERS];

main(){}

public OnGameModeInit()
{
    mysql_debug(1);
    mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_DB,MYSQL_PASS);
    DisableInteriorEnterExits();
    AllowInteriorWeapons(true);
    EnableStuntBonusForAll(false);
    AllowAdminTeleport(false);
    SetGameModeText("ParUni 4.0.0");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);

    //SetTimer("CheckMySQL", 30000, true); // Check MySQL Connection every 30 seconds.
    return 1;
}

public OnGameModeExit()
{
    mysql_close();
    return 1;
}
public OnPlayerConnect(playerid)
{
    new Query[200], Pname[24], escpname[24];\
    GetPlayerName(playerid, Pname, 24);
    mysql_real_escape_string(Pname, escpname);
    format(Query, sizeof(Query), "SELECT * FROM `Players` WHERE `user` = '%s'", escpname);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_num_rows() == 0) // This asks if the result was 0. Then his not registered
    {
        SendClientMessage(playerid,COLOR_BROWN,"Your Account hasnt been registered yet please do it now");
        ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Welcome on the Server","Please enter your Password below to register your account","Register","Close");
    }
    else
    {
       SendClientMessage(playerid,COLOR_BROWN,"Your Account has been found. Please login");
       ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Welcome on the Server","Your Account has been found","Login","Close");
    }
    mysql_free_result(); // clears the result
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    //SaveAccount(playerid); its commented cuz i dont know what you want to save or something like that
    LoggedIn[playerid] = 0;
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    new pname[24],escpname[24],escpass[24]; //string[128];
    if(dialogid == DIALOG_REGISTER)
    {
        if(!response)
        {
            SendClientMessage(playerid,COLOR_ERROR,"You have Aborted");
            Kick(playerid);
        }
        else if(response)
        {
            GetPlayerName(playerid,pname,24);
            mysql_real_escape_string(pname,escpname);
            mysql_real_escape_string(inputtext,escpass);
            format(query, sizeof(query), "INSERT INTO `Players` (`reg_id``Username`, `Password`) VALUES (NULL,'%s', sha1('%s'))", escpname, escpass);
            // Note: The Table and the Rows must be exactly named as shown up in phpmyadmin
            mysql_query(query);
            SendClientMessage(playerid,COLOR_GREEN,"You have been registerd. You may now login");
            ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Welcome on the Server","Your Account has been found","Login","Close");

        }
       
    }
    else if(dialogid == DIALOG_LOGIN)
    {
         if(!response)
        {
            SendClientMessage(playerid,COLOR_ERROR,"You have Aborted");
            Kick(playerid);
        }
        else if(response)
        {
            GetPlayerName(playerid,pname,24);
            mysql_real_escape_string(pname,escpname);
            mysql_real_escape_string(inputtext,escpass);
            format(query,sizeof(query),"SELECT * FROM `Players` WHERE Username = '%s' AND Password = sha1('%s')",escpname,escpass);
            mysql_query(query);
            mysql_store_result();
            if(!mysql_num_rows())
            {
                SendClientMessage(playerid,0x966C0FAA,"Wrong Password");
                ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Your account has been found","Welcome on Zombie Panic! Source","Login","Cancle");
                return 1;
            }
            else
            {
                new line[750];
                if(mysql_fetch_row(line)) //Fetches the line
                {
                   new data[3];//The data strings
                   new data2[5]; //The data variables
                   sscanf(line, "p|dss",data2[1], data[1],data[2]); //Splits the line with sscanf
                   mysql_free_result();
                   LoggedIn[playerid] = 1;
                   SendClientMessage(playerid, 0x009600AA, "You have been logged in!");
               }
            }
        }
    }
   
return 1;
}

stock sscanf(string[], format[], {Float,_}:...)
{
    #if defined isnull
        if (isnull(string))
    #else
        if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
    #endif
        {
            return format[0];
        }
    #pragma tabsize 4
    new
        formatPos = 0,
        stringPos = 0,
        paramPos = 2,
        paramCount = numargs(),
        delim = ' ';
    while (string[stringPos] && string[stringPos] <= ' ')
    {
        stringPos++;
    }
    while (paramPos < paramCount && string[stringPos])
    {
        switch (format[formatPos++])
        {
            case '\0':
            {
                return 0;
            }
            case 'i', 'd':
            {
                new
                    neg = 1,
                    num = 0,
                    ch = string[stringPos];
                if (ch == '-')
                {
                    neg = -1;
                    ch = string[++stringPos];
                }
                do
                {
                    stringPos++;
                    if ('0' <= ch <= '9')
                    {
                        num = (num * 10) + (ch - '0');
                    }
                    else
                    {
                        return -1;
                    }
                }
                while ((ch = string[stringPos]) > ' ' && ch != delim);
                setarg(paramPos, 0, num * neg);
            }
            case 'h', 'x':
            {
                new
                    num = 0,
                    ch = string[stringPos];
                do
                {
                    stringPos++;
                    switch (ch)
                    {
                        case 'x', 'X':
                        {
                            num = 0;
                            continue;
                        }
                        case '0' .. '9':
                        {
                            num = (num << 4) | (ch - '0');
                        }
                        case 'a' .. 'f':
                        {
                            num = (num << 4) | (ch - ('a' - 10));
                        }
                        case 'A' .. 'F':
                        {
                            num = (num << 4) | (ch - ('A' - 10));
                        }
                        default:
                        {
                            return -1;
                        }
                    }
                }
                while ((ch = string[stringPos]) > ' ' && ch != delim);
                setarg(paramPos, 0, num);
            }
            case 'c':
            {
                setarg(paramPos, 0, string[stringPos++]);
            }
            case 'f':
            {

                new changestr[16], changepos = 0, strpos = stringPos;
                while(changepos < 16 && string[strpos] && string[strpos] != delim)
                {
                    changestr[changepos++] = string[strpos++];
                    }
                changestr[changepos] = '\0';
                setarg(paramPos,0,_:floatstr(changestr));
            }
            case 'p':
            {
                delim = format[formatPos++];
                continue;
            }
            case '\'':
            {
                new
                    end = formatPos - 1,
                    ch;
                while ((ch = format[++end]) && ch != '\'') {}
                if (!ch)
                {
                    return -1;
                }
                format[end] = '\0';
                if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
                {
                    if (format[end + 1])
                    {
                        return -1;
                    }
                    return 0;
                }
                format[end] = '\'';
                stringPos = ch + (end - formatPos);
                formatPos = end + 1;
            }
            case 'u':
            {
                new
                    end = stringPos - 1,
                    id = 0,
                    bool:num = true,
                    ch;
                while ((ch = string[++end]) && ch != delim)
                {
                    if (num)
                    {
                        if ('0' <= ch <= '9')
                        {
                            id = (id * 10) + (ch - '0');
                        }
                        else
                        {
                            num = false;
                        }
                    }
                }
                if (num && IsPlayerConnected(id))
                {
                    setarg(paramPos, 0, id);
                }
                else
                {
                    #if !defined foreach
                        #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
                        #define __SSCANF_FOREACH__
                    #endif
                    string[end] = '\0';
                    num = false;
                    new
                        name[MAX_PLAYER_NAME];
                    id = end - stringPos;
                    foreach (Player, playerid)
                    {
                        GetPlayerName(playerid, name, sizeof (name));
                        if (!strcmp(name, string[stringPos], true, id))
                        {
                            setarg(paramPos, 0, playerid);
                            num = true;
                            break;
                        }
                    }
                    if (!num)
                    {
                        setarg(paramPos, 0, INVALID_PLAYER_ID);
                    }
                    string[end] = ch;
                    #if defined __SSCANF_FOREACH__
                        #undef foreach
                        #undef __SSCANF_FOREACH__
                    #endif
                }
                stringPos = end;
            }
            case 's', 'z':
            {
                new
                    i = 0,
                    ch;
                if (format[formatPos])
                {
                    while ((ch = string[stringPos++]) && ch != delim)
                    {
                        setarg(paramPos, i++, ch);
                    }
                    if (!i)
                    {
                        return -1;
                    }
                }
                else
                {
                    while ((ch = string[stringPos++]))
                    {
                        setarg(paramPos, i++, ch);
                    }
                }
                stringPos--;
                setarg(paramPos, i, '\0');
            }
            default:
            {
                continue;
            }
        }
        while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
        {
            stringPos++;
        }
        while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
        {
            stringPos++;
        }
        paramPos++;
    }
    do
    {
        if ((delim = format[formatPos++]) > ' ')
        {
            if (delim == '\'')
            {
                while ((delim = format[formatPos++]) && delim != '\'') {}
            }
            else if (delim != 'z')
            {
                return delim;
            }
        }
    }
    while (delim > ' ');
    return 0;
}
Reply
#5

When I connect with a unregistered account, it asks me to register.
After I've registered the account, I log off.
My PHPmyadmin shows the new made account, but when I log back on without a restart it asks me to register again. When I do, I see 2 accounts.
But if I relog after a restart it works properly.
The OnPlayerConnect is showed below.
Is it my OnPlayerConnect, or is it my account saving in register where I forgot something?
pawn Код:
public OnPlayerConnect(playerid)
{
    new Query[200], Pname[24], escpname[24];\
    GetPlayerName(playerid, Pname, 24);
    mysql_real_escape_string(Pname, escpname);
    format(Query, sizeof(Query), "SELECT * FROM `Players` WHERE `Username` = '%s'", escpname);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_num_rows() == 0) // This asks if the result was 0. Then his not registered
    {
        SendClientMessage(playerid,COLOR_BROWN,"Your Account hasnt been registered yet please do it now");
        ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Welcome on the Server","Please enter your Password below to register your account","Register","Close");
    }
    else
    {
       SendClientMessage(playerid,COLOR_BROWN,"Your Account has been found. Please login");
       ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Welcome on the Server","Your Account has been found","Login","Close");
    }
    mysql_free_result(); // clears the result
    return 1;
}
Login (dialog 1) and registration (dialog 2)
pawn Код:
case 1:
        {
            if(strlen(inputtext) == 0)
            {
                SendClientMessage(playerid, COLOR_ORANGE, "Wrong Password.");
                ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Authentication", "Welcome!\n\nPlease login by filling in your password.", "Login", "Cancel");
            }
            else
            {
                format(PlayerStats[playerid][Password], 36, inputtext);
                SaveAccount(playerid);
            }
        }
        case 2:
        {
            new Query[161];
            if(strlen(inputtext) < 4 || strlen(inputtext) > 30)
            {
                SendClientMessage(playerid, COLOR_SYSTEM, "Your password must be between 4 and 30 characters.");
                ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Registration", "Welcome!\n\nPlease enter a password to register.", "Register", "Cancel");
            }
            else
            {
                new EscapedName[MAX_PLAYER_NAME];
                GetPlayerName(playerid, EscapedName, MAX_PLAYER_NAME);

                format(Query, sizeof(Query), "INSERT INTO `Players` (Username, Password) VALUES('%s', '%s')", EscapedName, inputtext);
                mysql_query(Query);        
            }
        }
It's now the other way around than in my first post
Reply
#6

pawn Код:
format(Query, sizeof(Query), "INSERT INTO `Players` (Username, Password) VALUES('%s', '%s')", EscapedName, inputtext);
That's the first problem I've noticed. If you use the grave for a field/table name, you have to continue using it for other field and table names, you can't just use it once and then revert to the standard of not using them, so:

pawn Код:
format(Query, sizeof(Query), "INSERT INTO `Players` (`Username`, `Password`) VALUES('%s', '%s')", EscapedName, inputtext);
should work without a problem, I haven't fully read your post, but I assume that was apart of your problem as queries don't tend to send with that problem.
Reply
#7

The queries are being executed according to the log.
However, I don't see any rows in the database..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)