player stats won't save in database
#1

So I converted my gm that used Y_INI to SQLite and when I go ingame I register log out close the server and then put it on again and it asks me to register again. Now I check the scriptfiles for the db file and it contains 0 kbs.

here is my code:
pawn Код:
enum USER_DATA
{
    USER_ID,
    USER_NAME[MAX_PLAYER_NAME],
    USER_PASSWORD[129],
    USER_SCORE,
    USER_ADMIN,
    USER_VIP,
    USER_KILLS,
    USER_DEATHS,
   
    bool: USER_LOGGED_IN
}
new User[MAX_PLAYERS][USER_DATA];

public OnGameModeInit()
{
    SetGameModeText("Team Deathmatch");
    AddPlayerClass(287, 167.9983,1849.0885,33.8984, 90, -1, -1, -1, -1, -1, -1);
    AddPlayerClass(78, -166.3599,1222.5267,19.7422, 90, -1, -1, -1, -1, -1, -1);
    Database = db_open("server.db");
    db_query(Database, "PRAGMA synchronous = OFF");
    new Query[256] =
    "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, password VARCHAR(129), score INTEGER DEFAULT 0 NOT NULL,";
    strcat(Query, "admin INTEGER DEFAULT 0 NOT NULL, vip INTEGER DEFAULT 0 NOT NULL, kills INTEGER DEFAULT 0 NOT NULL, deaths INTEGER DEFAULT 0 NOT NULL", sizeof(Query));
    db_query(Database, Query);
    return 1;
}

public OnGameModeExit()
{
    db_close(Database);
    return 1;
}

public OnPlayerConnect(playerid)
{
    for( new i; i < _: USER_DATA; ++i ) User[ playerid ][ USER_DATA: i ] = 0;
   
    GetPlayerName(playerid, User[playerid][USER_NAME], MAX_PLAYER_NAME);
   
    new
        Query[32]
    ;
    format(Query, sizeof(Query), "SELECT password FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
    Result = db_query(Database, Query);
    if(db_num_rows(Result))
    {
        db_get_field_assoc(Result, "password", User[playerid][USER_PASSWORD], 129);
        ShowPlayerDialog(playerid, dlogin, DIALOG_STYLE_PASSWORD, "Login", "Write in your password to login in.", "Login", "Quit");
    }
    else ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_PASSWORD, "Register", "Write in your password to register.", "Register", "Quit");
    db_free_result(Result);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(User[playerid][USER_LOGGED_IN] == true)
    {
        new
            Query[128]
        ;
        format(Query, sizeof(Query), "UPDATE users SET score = %d, admin = %d, vip = %d, kills = %d, deaths = %d WHERE username '%s'", User[playerid][USER_SCORE], User[playerid][USER_ADMIN], User[playerid][USER_VIP], User[playerid][USER_KILLS], User[playerid][USER_DEATHS], DB_Escape(User[playerid][USER_NAME]));
        db_query(Database, Query);
    }
   
    for( new i; i < _: USER_DATA; ++i ) User[ playerid ][ USER_DATA: i ] = 0;
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == dregister)
    {
        if(!response) return Kick(playerid);
        if(response)
        {
            if(!IsValidPassword(inputtext))
            {
                SendClientMessage(playerid, -1, "The password is invalid. Valid characters are: A-Z, a-z, 0-9" );
                ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_PASSWORD, "Register", "Write your password to register.", "Register", "Quit");
                return 1;
            }
            if(strlen(inputtext) < 3 || strlen(inputtext) > 24)
            {
                SendClientMessage( playerid, -1, "[ERROR]: The password is invalid. Its lenght should be 3-24 characters" );
                ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_PASSWORD, "Register", "Write your password to register.", "Register", "Quit");
                return 1;
            }
            new
                Query[208]
            ;
            WP_Hash(User[playerid][USER_PASSWORD], 129, inputtext);
            format(Query,sizeof(Query),"INSERT INTO users (username, password) VALUES ('%s', '%s')", DB_Escape( User[ playerid ][ USER_NAME ] ), DB_Escape( User[ playerid ][ USER_PASSWORD ] ) );
            db_query(Database, Query);
           
            User[playerid][USER_LOGGED_IN] = true;
            SendClientMessage(playerid, -1, "Thank you for registering to our server! You have been automatically logged in!");
        }
    }
    if(dialogid == dlogin)
    {
        if(!response) return Kick(playerid);
        if(response)
        {
            new
                buf[129]
            ;
            WP_Hash(buf, 129, inputtext);
            if(!strcmp(buf,User[playerid][USER_PASSWORD],false))
            {
                new Query[75];
                format( Query, sizeof( Query ), "SELECT * FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape( User[ playerid ][ USER_NAME ] ) );
                Result = db_query( Database, Query );
                if( db_num_rows( Result ) )
                {
                    db_get_field_assoc( Result, "userid", Query, 7 );
                    User[ playerid ][ USER_ID ] = strval( Query );

                    db_get_field_assoc(Result, "score", Query, 6);
                    User[playerid][USER_SCORE] = strval(Query);
                    SetPlayerScore(playerid, User[playerid][USER_SCORE]);

                    db_get_field_assoc( Result, "admin", Query, 3 );
                    User[ playerid ][ USER_ADMIN ] = strval( Query );
                   
                    db_get_field_assoc(Result, "vip", Query, 3);
                    User[playerid][USER_VIP] = strval(Query);
                   
                    db_get_field_assoc(Result, "kills", Query, 6);
                    User[playerid][USER_KILLS] = strval(Query);
                   
                    db_get_field_assoc(Result, "deaths", Query, 6);
                    User[playerid][USER_DEATHS] = strval(Query);

                    User[ playerid ][ USER_LOGGED_IN ] = true;
                    SendClientMessage( playerid, -1, "[SERVER]: You have successfully logged in to your account!" );
                }
                db_free_result( Result );
            }
            else
            {
                SendClientMessage( playerid, 0xFF0000FF, "[ERROR]: Incorrect password" );
                ShowPlayerDialog( playerid, dlogin, DIALOG_STYLE_PASSWORD, "Login", "Write your password to login.", "Login", "Quit" );
            }
        }
    }
        return 1;
}
any help would be appreciated
Reply
#2

You forgot a equal sign between username and '%s' so try this.

Код:
public OnPlayerDisconnect(playerid, reason)
{
	if(User[playerid][USER_LOGGED_IN] == true)
	{
		new
		    Query[128]
		;
	    format(Query, sizeof(Query), "UPDATE users SET score = %d, admin = %d, vip = %d, kills = %d, deaths = %d WHERE username = '%s'", User[playerid][USER_SCORE], User[playerid][USER_ADMIN], User[playerid][USER_VIP], User[playerid][USER_KILLS], User[playerid][USER_DEATHS], DB_Escape(User[playerid][USER_NAME]));
	    db_query(Database, Query);
	}
	
	for( new i; i < _: USER_DATA; ++i ) User[ playerid ][ USER_DATA: i ] = 0;
	return 1;
}
Reply
#3

The problem has been solved, awesomedude helped me.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)