mysql doesn't save stats on disconnect
#1

Hey, I recently followed a tutorial about making a register and login script in mysql to see how it works. PAWNO connects succefully to the database and writes in all the data. But when I log back in and out my money nor my postion is getting saved. Can anybody help me with my problem?

Код:
//INCLUDES
#include <a_samp>
#include <a_mysql>
#include <zcmd>

//WP PASSWORD HASH
native WP_Hash(buffer[], len, const str[]);

//DATABASE
#define    MYSQL_HOST        "localhost"
#define    MYSQL_USER        "root"
#define    MYSQL_DATABASE    "narp"
#define    MYSQL_PASSWORD    ""

//SPAWN DEFINE
#define    SPAWN_X  10.0
#define    SPAWN_Y  10.0
#define    SPAWN_Z  14.0
#define    SPAWN_A  0.0

//NEW
new mysql;

//DIALOGS
enum
{
	DIALOG_REGISTER,
    DIALOG_LOGIN
};

//PLAYER DATA
enum PlayerData
{
    ID,
    Name[MAX_PLAYER_NAME],
    Password[129],
    IP[16],
    Admin,
    VIP,
    Money,
    Float:posX,
    Float:posY,
    Float:posZ,
    Float:posA
};
new Player[MAX_PLAYERS][PlayerData];

public OnGameModeInit()
{
    mysql = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);
    if(mysql_errno() != 0)
    {
        printf("[MySQL] The connection has failed.");
    }
    else
    {
        printf("[MySQL] The connection was successful.");
    }
    return true;
}

//ACCOUNT CHECK
forward OnAccountCheck(playerid);
public OnAccountCheck(playerid)
{
    new
        rows,
        fields;
    cache_get_data(rows, fields, mysql);

    if(rows)
    {
        cache_get_field_content(0, "Password", Player[playerid][Password], mysql, 129);
        Player[playerid][ID] = cache_get_field_content_int(0, "ID");
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "Welcome player!\nYour account has been found in our database. Please fill in your password:", "Login", "Quit");
    }
    else
    {
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Register", "Welcome player!\nYour account has not been registered yet. Please fill in your desired password:", "Register", "Quit");
    }
    return true;
}

//ACCOUNT LOAD
forward OnAccountLoad(playerid);
public OnAccountLoad(playerid)
{
    Player[playerid][Admin] = cache_get_field_content_int(0, "Admin");
    Player[playerid][VIP] = cache_get_field_content_int(0, "VIP");
    Player[playerid][Money] = cache_get_field_content_int(0, "Money");
    Player[playerid][posX] = cache_get_field_content_float(0, "PosX");
    Player[playerid][posY] = cache_get_field_content_float(0, "PosY");
    Player[playerid][posZ] = cache_get_field_content_float(0, "PosZ");
    Player[playerid][posA] = cache_get_field_content_float(0, "PosA");

    TogglePlayerSpectating(playerid, false);

    GivePlayerMoney(playerid, Player[playerid][Money]);

    SetSpawnInfo(playerid, 0, 23, Player[playerid][posX], Player[playerid][posY], Player[playerid][posZ], Player[playerid][posA], 0, 0, 0, 0, 0, 0);
    SpawnPlayer(playerid);

    SendClientMessage(playerid, -1, "You have successfully logged in.");
    return true;
}

//ACCOUNT REGISTER
forward OnAccountRegister(playerid);
public OnAccountRegister(playerid)
{
    Player[playerid][ID] = cache_insert_id();
    printf("[Registration] New account registered. Database ID: [%d]", Player[playerid][ID]);

    TogglePlayerSpectating(playerid, false);

    SetSpawnInfo(playerid, 0, 23, SPAWN_X, SPAWN_Y, SPAWN_Z, SPAWN_A, 0, 0, 0, 0, 0, 0);
    SpawnPlayer(playerid);
    return true;
}

public OnPlayerConnect(playerid)
{
    TogglePlayerSpectating(playerid, true);
    new
        query[128],
        playername[MAX_PLAYER_NAME];

    GetPlayerName(playerid, playername, sizeof(playername));
    mysql_format(mysql, query, sizeof(query), "SELECT `Password`, `ID` FROM `accounts` WHERE `Name` = '%e' LIMIT 1", playername);
    mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid);
    return true;
}

public OnPlayerDisconnect(playerid, reason)
{
    new
        query[128],
        Float:pos[4];

    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
    GetPlayerFacingAngle(playerid, pos[3]);

    mysql_format(mysql, query, sizeof(query), "UPDATE `accounts` SET `Money` = %d, `PosX` = %f, `PosY` = %f, `PosZ` = %f, `PosA` = %f WHERE `ID` = %d",
    GetPlayerMoney(playerid), pos[0], pos[1], pos[2], pos[3], Player[playerid][ID]);
    mysql_tquery(mysql, query, "", "");
    return true;
}

public OnPlayerSpawn(playerid)
{
    return true;
}

CMD:money(playerid, params[])
{
 	GivePlayerMoney(playerid, 10000);
	return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_LOGIN:
        {
            if(!response) Kick(playerid);

            new
                hashpass[129],
                query[100],
                playername[MAX_PLAYER_NAME];

            GetPlayerName(playerid, playername, sizeof(playername));
            WP_Hash(hashpass, sizeof(hashpass), inputtext);
            if(!strcmp(hashpass, Player[playerid][Password]))
            {
                mysql_format(mysql, query, sizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e' LIMIT 1", playername);
                mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid);
            }
            else
            {
                SendClientMessage(playerid, -1, "You have specified an incorrect password!");
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "Welcome player!\nYour account has been found in our database. Please fill in your password:", "Login", "Quit");
            }
        }
        case DIALOG_REGISTER:
        {
            if(!response) return Kick(playerid);
            if(strlen(inputtext) < 5)
            {
                SendClientMessage(playerid, -1, "Your password must at least contain more than 4 characters.");
                return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Register", "Welcome player!\nYour account has not been registered yet. Please fill in your desired password:", "Register", "Quit");
            }
            new
                query[512],
                playername[MAX_PLAYER_NAME],
                playerip[16];


            GetPlayerName(playerid, playername, sizeof(playername));
            GetPlayerIp(playerid, playerip, sizeof(playerip));
            WP_Hash(Player[playerid][Password], 129, inputtext);
            mysql_format(mysql, query, sizeof(query), "INSERT INTO `accounts` (`Name`, `Password`, `IP`, `Admin`, `VIP`, `Money`, `PosX`, `PosY`, `PosZ`, `PosA`) VALUES ('%e', '%e', '%e', 0, 0, 0, %f, %f, %f, %f)", playername, Player[playerid][Password], playerip, SPAWN_X, SPAWN_Y, SPAWN_Z, SPAWN_A);
            mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
        }
    }
    return false;
}
Reply
#2

Did u follow exactly the steps ? i use the same login& register system and i do not have any problem with it .
Reply
#3

Yes I actually did. That's very weird.
Reply
#4

*SOLVED

I didn't include:
main()
{

}

This gave an error in the console.
Reply
#5

What mysql log says?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)