Can't register on MYSQL
#1

Hello

I have a problem about MySQL.

https://sampforum.blast.hk/showthread.php?tid=56564 I use G-sTyLeZzZ plugin.

I can't register user on my server using Mysql R5 and SA-MP 0.3b R2.

Script from https://sampforum.blast.hk/showthread.php?tid=159785

pawn Код:
#include <a_samp>
//Gstyles include
#include <a_mysql>
//DCMD (Will be used for our register and login commands later in the tutorial
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
//The following information is only for our wamp server, when we are ready to upload it to our server for use you will need to change it
#define SQL_HOST "localhost" //Standard wamp host
#define SQL_USER "root" //Standard wamp user
#define SQL_PASS "070798132" //Standard wamp password (Nothing)
#define SQL_DB "tesregis" //This is the database we are going to use (I'll show you how to set it up later :) )
#define mysql_fetch_row(%1) mysql_fetch_row_format(%1,"|")

//I credit whoever made this on sa-mp market :P
public OnFilterScriptInit()
{
    if(!mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))
    {
        print("SQL connection attempt 1 FAILED!");
        if(!mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))
        {
            print("SQL connection attempt 2 FAILED!");
            if(!mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))
            {
                print("SQL connection attempt 3 FAILED!");
                return 1;
            }
        }
    }
    return 1;
}

public OnPlayerConnect(playerid)
{
    new Query[200], Pname[24]; //Creates the Query string and the Pname
    GetPlayerName(playerid, Pname, 24); //Gets the players name
    format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s'", Pname); //Selects the line where the playername is the player
    mysql_query(Query); //Querys the string
    mysql_store_result(); //Stores the result
    if(mysql_num_rows() != 0) //If the user is found
    {
        new PIP[56]; //Creates the IP string
        GetPlayerIp(playerid, PIP, 56); //Gets the players IP
        format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s' AND `IP` = '%s'", Pname, PIP); //Checks if the players IP is the same, if so it will auto login :)
        mysql_query(Query); //Queries the result
        mysql_store_result(); //Stores the result
        if(mysql_num_rows() != 0) // If the players IP is the same in the DB
        {
            new line[750];
            SetPVarInt(playerid, "Logged", 1); //Sets the logged variable to 1
            SendClientMessage(playerid, 0x009600AA, "Auto Logged in!");
            if(mysql_fetch_row(line)) //Fetches the line
            {
                   new data[3][50]; //The data strings
                   new data2[4]; //The data variables
                   sscanf(line, "p|ssdddds", data[0], data[1], data2[0], data2[1], data2[2], data2[3], data[2]); //Splits the line with sscanf
                   SetPVarInt(playerid, "Kills", data2[0]); //Sets Pvar ints
                   SetPVarInt(playerid, "Logged", 1); //Sets Pvar ints
                   SetPVarInt(playerid, "Deaths", data2[1]); //Sets Pvar ints
                   SetPlayerScore(playerid, data2[2]); //Sets players score
                   GivePlayerMoney(playerid, data2[3]); //Sets players cash
                   mysql_free_result();
               }
        }
        if(!mysql_num_rows())
        {
            SendClientMessage(playerid, 0x009600AA, "This account is registered, please login"); //User is registered but IP does not match
            ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel");
        }
    }
    else
    {
        ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel");
        SendClientMessage(playerid, 0x009600AA, "This account is not registered, please register!"); // User is not registed
    }
    mysql_free_result(); //Frees the result :)
    return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 14600)
    {
       if(response)
       {
            if(!strlen(inputtext)) //If the player doesn't enter a pass
            {
                ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel");
                SendClientMessage(playerid, 0xF60000AA, "Please enter a password");
            }
            new PIP[50];
            new Query[200], Pname[24];
            GetPlayerName(playerid, Pname, 24); //Players name
            GetPlayerIp(playerid, PIP, 50); //Players IP
            format(Query, sizeof(Query), "INSERT INTO `playerinfo` (`user`, `password`, `kills`, `deaths`, `score`, `money`, IP) VALUES ('%s', '%s', 0, 0, 0, 0, '%s')", Pname, inputtext, PIP); //Insert string
            mysql_query(Query); //Queries
            GameTextForPlayer(playerid, "~g~Registered", 2000, 3);
            SendClientMessage(playerid, 0x0000D9AA, "Registered and Logged into your account!");
            SetPVarInt(playerid, "Logged", 1);
        }
    }
    if(dialogid == 15000)
    {
       if(response)
       {
           new Query[200], Pname[24];
           GetPlayerName(playerid, Pname, 24);
           format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s' AND `password` = '%s'", Pname, inputtext);
           mysql_query(Query);
           mysql_store_result();
           if(!mysql_num_rows())
           {
               SendClientMessage(playerid, 0xF60000AA, "Invalid password!");
               SetPVarInt(playerid, "WrongPass", GetPVarInt(playerid, "WrongPass") + 1);
               ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel");
               if(GetPVarInt(playerid, "WrongPass") == 3)
               {
                   SendClientMessage(playerid, 0xF60000AA, "Max password tries exceeded!!");
                   Kick(playerid);
               }
           }
           else
           {
               new line[750]; //I know it's big D=
               if(mysql_fetch_row(line)) //Fetches the line
               {
                   new data[3][50]; //The data strings
                   new data2[4]; //The data variables
                   sscanf(line, "p|ssdddds", data[0], data[1], data2[0], data2[1], data2[2], data2[3], data[2]); //Splits the line with sscanf
                   SetPVarInt(playerid, "Kills", data2[0]); //Sets Pvar ints
                   SetPVarInt(playerid, "Logged", 1); //Sets Pvar ints
                   SetPVarInt(playerid, "Deaths", data2[1]); //Sets Pvar ints
                   SetPlayerScore(playerid, data2[2]); //Sets players score
                   SetPVarInt(playerid, "MoneyGiven", data2[3]); //Sets players cash
                   SendClientMessage(playerid, 0x0000D9AA, "Logged in!");
                   mysql_free_result();
               }
           }
       }
    }
    return 1;
}

public OnPlayerRequestSpawn(playerid)
{
    if(GetPVarInt(playerid, "Logged") == 0)
    {
        new Query[200], Pname[24]; //Variables
        GetPlayerName(playerid, Pname, 24); //Gets the player name
        format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s'" , Pname); //MySQL string
        mysql_query(Query); //Queries
        mysql_store_result(); //Stores
        if(!mysql_num_rows()) ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel"); //If the user is not found it will show the register dialog
        else ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel"); //If the user is not found it will show the login dialog
        return 0; //Player won't spawn
    }
    return 1;
}

public OnPlayerSpawn(playerid)
{
    if(GetPVarInt(playerid, "Logged") == 1 && GetPVarInt(playerid, "MoneyGiven") > 0)
    {
        GivePlayerMoney(playerid, GetPVarInt(playerid, "MoneyGiven")); //Cant give cash on OnPlayerConnect
        SetPVarInt(playerid, "MoneyGiven", 0);
    }
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    SetPVarInt(playerid, "Deaths", GetPVarInt(playerid, "Deaths") + 1); //Adds a death to the person who died
    if(killerid != INVALID_PLAYER_ID) SetPVarInt(playerid, "Kills", GetPVarInt(playerid, "Kills") + 1); // Adds a kill to the killer unless the player suicided.
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(GetPVarInt(playerid, "Logged") == 1) //Only saves if the player was logged in
    {
        new query[300]; //Variables
        new Pname[24]; //Variables
        GetPlayerName(playerid, Pname, 24); //Gets the player name
        format(query, sizeof(query), "UPDATE `playerinfo` SET `score` = '%d',`money` = '%d', `kills` = '%d', `deaths` = '%d'  WHERE `user` = '%s'", GetPlayerScore(playerid), GetPlayerMoney(playerid), GetPVarInt(playerid, "Kills"), GetPVarInt(playerid, "Deaths"), Pname); //Saves all the info
        mysql_query(query); //queries
        mysql_free_result(); //Frees the result
    }
    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;
}
When i registed and rejoin me must register again for this script.

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

forward OnUserRegister(query[], index, extraid, connectionHandle);
forward OnConnectResponse(query[], index, extraid, connectionHandle);
forward OnUserLogin(query[], index, extraid, connectionHandle);
forward OnUserUpdate(query[], index, extraid, connectionHandle);

//MySQL Configuration
#define SQL_HOST "host"
#define SQL_DB "database"
#define SQL_USER "username"
#define SQL_PASS "password"

#define TABLENAME "users"

#define GREY 0xAFAFAFAA
#define RED 0xFF0000AA
#define YELLOW 0xFFFF00AA
#define LIGHTBLUE 0x33CCFFAA

#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1

//global variables
new
    bool:LoggedIn[MAX_PLAYERS] = { false, ... },
    bool:AccRegistered[MAX_PLAYERS] = { false, ... },
    Wrongattempt[MAX_PLAYERS],
    TimerSet[MAX_PLAYERS],
    pLogtimer[MAX_PLAYERS],
    PlayerMoney[MAX_PLAYERS];

public OnFilterScriptInit()
{
    mysql_debug(1);
    mysql_connect(SQL_HOST, SQL_USER,SQL_DB, SQL_PASS);
    SetupTable(); //run this only once
}

SetupTable()
{
    return mysql_query("CREATE TABLE IF NOT EXISTS `"TABLENAME"`(`id` int(11) NOT NULL auto_increment PRIMARY KEY,`Username` varchar(30) NOT NULL,`Password` varchar(50) NOT NULL,`Money` int(10) NOT NULL default '0')");
}

public OnFilterScriptExit()
{
    printf("OnFilterScriptExit()");
    mysql_close();
}

RegisterAccount(playerid,pass[])
{
    new
        pName[MAX_PLAYER_NAME],
        query[256];

    GetPlayerName(playerid,pName,sizeof(pName));
    mysql_real_escape_string(pName,pName);
    mysql_real_escape_string(pass,pass);
    format(query,sizeof(query),"INSERT INTO `"TABLENAME"` (Username,Password) VALUES ('%s',md5('%s'))",pName,pass);
    mysql_query_callback(playerid,query,"OnUserRegister");
    return 1;
}

public OnUserRegister(query[], index, extraid, connectionHandle)
{
    new string[128],pName[MAX_PLAYER_NAME];
    if(IsPlayerConnected(index)) {
        GetPlayerName(index,pName,sizeof pName);
        format(string,sizeof(string),">> Account %s successfully registered - Remember your password for later use.",pName);
        SendClientMessage(index,GREY,string);
        SendClientMessage(index,GREY,"You have been automatically logged in");
        LoggedIn[index] = true;
        AccRegistered[index] = true;
    }
    return 1;
}

LoginPlayer(playerid,pass[])
{
    new
        pName[MAX_PLAYER_NAME],
        query[256];

    GetPlayerName(playerid,pName,sizeof(pName));

    mysql_real_escape_string(pName,pName);
    mysql_real_escape_string(pass,pass);
    format(query,sizeof(query),"SELECT Money FROM `"TABLENAME"` WHERE Username = '%s' AND Password = md5('%s') LIMIT 1",pName,pass);
    mysql_query_callback(playerid,query,"OnUserLogin");
    return 1;
}

public OnUserLogin(query[], index, extraid, connectionHandle)
{
    new string[128],pName[MAX_PLAYER_NAME];
    if(IsPlayerConnected(index)) {
        mysql_store_result();
        if(mysql_num_rows() == 1) {
            PlayerMoney[index] = mysql_fetch_int();
            GivePlayerMoney(index,PlayerMoney[index]);
            LoggedIn[index] = true;
            format(string,sizeof(string),">> You have been successfully logged in. (Money: %d)",PlayerMoney[index]);
            SendClientMessage(index,GREY,string);
            mysql_free_result();
        } else {
            Wrongattempt[index] += 1;
            printf("Bad log in attempt by %s (Total attempts: %d)",pName,Wrongattempt[index]);
            if(Wrongattempt[index] >= 3)    {
                SendClientMessage(index,RED,">> You have been kicked.( 3 times wrong pass )");
                mysql_free_result();
                return Kick(index);
            }
            mysql_free_result();
            SendClientMessage(index,RED,">> Wrong Password");
        }
    } else {
        //to avoid "commands out of sync" errors
        mysql_store_result();
        mysql_free_result();
    }
    return 1;
}


public OnPlayerDisconnect(playerid,reason)
{
    if(pLogtimer[playerid] != 0) KillTimer(pLogtimer[playerid]);
    new
        query[300],
        pName[MAX_PLAYER_NAME];

    GetPlayerName(playerid,pName,sizeof(pName));

    if(LoggedIn[playerid]) {
        new Float:arm;
        GetPlayerArmour(playerid,arm);
        format(query,sizeof(query),"UPDATE `"TABLENAME"` SET `Money`='%d' WHERE (`Username` = '%s')",GetPlayerMoney(playerid),pName);
        mysql_query_callback(playerid,query,"OnUserUpdate");
    }
    return 1;
}

public OnUserUpdate(query[], index, extraid, connectionHandle)
{
    printf("Data of playerid %d has been updated",index);
    return 1;
}

public OnPlayerConnect(playerid)
{
    new
        query[256],
        pname[MAX_PLAYER_NAME];
    Wrongattempt[playerid] = 0;
    LoggedIn[playerid] = false;
    TimerSet[playerid] = 0;

    GetPlayerName(playerid,pname,sizeof(pname));
    format(query,sizeof(query),"SELECT * FROM `"TABLENAME"` WHERE Username = '%s'",pname);
    mysql_query_callback(playerid,query,"OnConnectResponse");
    return 1;
}

public OnConnectResponse(query[], index, extraid, connectionHandle)
{
    new string[128],pName[MAX_PLAYER_NAME];
    if(IsPlayerConnected(index))  {
        GetPlayerName(index,pName,sizeof pName);
        mysql_store_result();
        if(mysql_num_rows() > 0) {
            format(string,sizeof(string),">> This account (%s) is registered.Please login by using /login [pass]",pName);
            SendClientMessage(index,GREY,string);
            AccRegistered[index] = true;
            pLogtimer[index] = SetTimerEx("LoginKick",30000,0,"d",index);
        } else {
            format(string,sizeof(string),">> Welcome %s, you can register by using /register [pass]",pName);
            SendClientMessage(index,GREY,string);
            AccRegistered[index] = false;
        }
        mysql_free_result();
    } else {
        //to avoid "commands out of sync" errors
        mysql_store_result();
        mysql_free_result();
    }
    return 1;
}

forward LoginKick(playerid);
public LoginKick(playerid)
{
    if(!LoggedIn[playerid]) KickEx(playerid,"Not logged in");
    else
    {
        KillTimer(pLogtimer[playerid]);
        pLogtimer[playerid] = 0;
    }
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(register,8,cmdtext);
    dcmd(login,5,cmdtext);
    return 0;
}

dcmd_login(playerid, params[])
{
    if(LoggedIn[playerid])
    {
        return SendClientMessage(playerid,RED,">> You are already logged in");
    }
    if(!AccRegistered[playerid])
    {
        return SendClientMessage(playerid,RED,">> This Account is not registered. ( Use /register [pass] )");
    }
    if(!strlen(params))
    {
        return SendClientMessage(playerid,RED,"SYNTAX: /login [password]");
    }
    LoginPlayer(playerid,params);
    return true;
}

dcmd_register(playerid, params[])
{
    new pName[MAX_PLAYER_NAME];
    GetPlayerName(playerid,pName,sizeof(pName));
    if(AccRegistered[playerid])
    {
        return SendClientMessage(playerid,RED,">> This account is already registered. ( /login [pass] )");
    }
    if(LoggedIn[playerid])
    {
        return SendClientMessage(playerid,RED,">> You are already logged in");
    }
    if(!strlen(params))
    {
        return SendClientMessage(playerid,RED,"SYNTAX: /register [password]");
    }
    if(strlen(params) < 6)
    {
        return SendClientMessage(playerid,RED,">> The password should contain 6 characters at least.");
    }
    RegisterAccount(playerid,params);
    return 1;
}

stock KickEx(playerid,reason[])
{
    new
        string[1000],
        MsgAll[128],
        pName[MAX_PLAYER_NAME];
    GetPlayerName(playerid,pName,sizeof(pName));
    format(string,sizeof(string),"You have been kicked: ");
    strcat(string,reason,sizeof(string));
    SendClientMessage(playerid,RED,string);
    Kick(playerid);
    format(MsgAll,sizeof(MsgAll),">> %s has been kicked.(Reason: %s)",pName,reason);
    SendClientMessageToAll(GREY,MsgAll);
    return 1;
}
For this script when i registed server will close no reason.

How to solve ?

Sorry for bad english
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)