Not saving stats [MYSQL]
#1

Hi! I'm new with scripting, and I'm trying to learn more and more
But I can't figure it out how can I save my stats
I've already tried everything, but I can't success
Here's the code:

Quote:

/ This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

#include <a_samp>
#include <YSI\y_ini>
#include <zcmd>
#include <sscanf2>
#include <a_mysql>

#define skinid 137
#define RULES_MENU 25

#define DIALOG_HELP 5
#define DIALOG_STATS 6


#define COL_WHITE "{FFFFFF}"
#define COL_RED "{F81414}"
#define COL_GREEN "{00FF22}"
#define COL_LIGHTBLUE "{00CED1}"
#define COL_PINK "{FF00EA}"


#define SPAWN_X 10.0
#define SPAWN_Y 10.0
#define SPAWN_Z 14.0
#define SPAWN_A 0.0

#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_DATABASE "NLSP"
#define MYSQL_PASSWORD ""

#define DIALOG_REGISTER (0)
#define DIALOG_LOGIN (1)

native WP_Hash(buffer[], len, const str[]);

enum
{
LoginDialog,
RegisterDialog,
DialogHelp
};

new mysql;

enum PlayerData
{
ID,
Name[MAX_PLAYER_NAME],
Password[129],
IP[16],
Admin,
Premium,
Money,
FloatosX,
FloatosY,
FloatosZ,
FloatosA,
Kills,
Deaths,
Cache: Cache_ID,
bool: IsLoggedIn
};
new Player[MAX_PLAYERS][PlayerData];

stock GetName(playerid)
{
new szName[MAX_PLAYER_NAME];
GetPlayerName(playerid, szName, sizeof(szName));
return szName;
}

main() {}

public OnGameModeInit()
{
mysql_log(LOG_ALL);
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;
}

public OnGameModeExit()
{
return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
SpawnPlayer(playerid);
}

public OnPlayerConnect(playerid)
{
TogglePlayerSpectating(playerid, true);
new query[512],
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[512],
Floatos[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', `Kills` = '%d', `Deaths` = '%d', WHERE `ID` = '%d'", Player[playerid][Deaths], Player[playerid][Kills]);
mysql_tquery(mysql, query, "OnPlayerUpdate", "i", playerid);
return true;
}

public OnPlayerSpawn(playerid)
{
return true;
}

public OnPlayerDeath(playerid, killerid, reason)
{
Player[playerid][Deaths]++;
return 1;
}

public OnVehicleSpawn(vehicleid)
{
return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
return 1;
}

public OnPlayerText(playerid, text[])
{
return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
return 1;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
return 1;
}

public OnPlayerEnterCheckpoint(playerid)
{
return 1;
}

public OnPlayerLeaveCheckpoint(playerid)
{
return 1;
}

public OnPlayerEnterRaceCheckpoint(playerid)
{
return 1;
}

public OnPlayerLeaveRaceCheckpoint(playerid)
{
return 1;
}

public OnRconCommand(cmd[])
{
return 1;
}

public OnPlayerRequestSpawn(playerid)
{
return 1;
}

public OnObjectMoved(objectid)
{
return 1;
}

public OnPlayerObjectMoved(playerid, objectid)
{
return 1;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
return 1;
}

public OnVehicleMod(playerid, vehicleid, componentid)
{
return 1;
}

public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
{
return 1;
}

public OnVehicleRespray(playerid, vehicleid, color1, color2)
{
return 1;
}

public OnPlayerSelectedMenuRow(playerid, row)
{
return 1;
}

public OnPlayerExitedMenu(playerid)
{
return 1;
}

public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
return 1;
}

public OnRconLoginAttempt(ip[], password[], success)
{
return 1;
}

public OnPlayerUpdate(playerid)
{
SetPlayerScore(playerid, GetPlayerMoney(playerid));
return 1;
}

public OnPlayerStreamIn(playerid, forplayerid)
{
return 1;
}

public OnPlayerStreamOut(playerid, forplayerid)
{
return 1;
}

public OnVehicleStreamIn(vehicleid, forplayerid)
{
return 1;
}

public OnVehicleStreamOut(vehicleid, forplayerid)
{
return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case LoginDialog:
{
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, ""COL_RED"Incorrect password!");
ShowPlayerDialog(playerid, LoginDialog, DIALOG_STYLE_INPUT, "Login", ""COL_LIGHTBLUE"Welcome!\nPlease login:", "Login", "Quit");
}
}
case RegisterDialog:
{
if(!response) return Kick(playerid);
if(strlen(inputtext) < 5)
{
SendClientMessage(playerid, -1, ""COL_RED"Your password must at least contain more than 4 characters.");
return ShowPlayerDialog(playerid, RegisterDialog, DIALOG_STYLE_INPUT, "Register", ""COL_LIGHTBLUE"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`, `Premium`, `Money`, `PosX`, `PosY`, `PosZ`, `PosA`, `Kills`, `Deaths`) VALUES ('%e', '%e', '%e', 0, 0, 0, %f, %f, %f, %f , %d, %d)", playername, Player[playerid][Password], playerip, SPAWN_X, SPAWN_Y, SPAWN_Z, SPAWN_A, Player[playerid][Kills], Player[playerid][Deaths]);
mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid);
}
}
return 1;
}



public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
return 1;
}

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, LoginDialog, DIALOG_STYLE_INPUT, "Login", ""COL_LIGHTBLUE"Welcome!\nYou are registered, please fill your password in below:", "Login", "Quit");
}
else
{
ShowPlayerDialog(playerid, RegisterDialog, DIALOG_STYLE_INPUT, "Register", ""COL_LIGHTBLUE"Welcome player!\nYour account has not been registered yet. Please fill in your desired password:", "Register", "Quit");
}
return true;
}


forward OnAccountLoad(playerid);
public OnAccountLoad(playerid)
{
Player[playerid][Admin] = cache_get_field_content_int(0, "Admin");
Player[playerid][Premium] = cache_get_field_content_int(0, "Premium");
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");
Player[playerid][Kills] = cache_get_field_content_int(0, "Kills");
Player[playerid][Deaths] = cache_get_field_content_int(0, "Deaths");
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, ""COL_PINK"You have successfully logged in.");
return true;
}


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;
}


CMD:kill(playerid, params[])
{
SetPlayerHealth(playerid, 0);
SetPlayerArmour(playerid, 0);
SendClientMessage(playerid, -1, ""COL_RED"You killed yourself!");
return 1;
}
CMD:cash(playerid, params[])
{
GivePlayerMoney(playerid, 282004);
}

CMD:updates(playerid, params[])
{
ShowPlayerDialog(playerid,2,DIALOG_STYLE_MSGBOX,"" COL_PINK"Updates","Update 1: New Features, \nUpdate 2: New Vehicles, \nUpdate 3: Best update command","Okay",""); // you can change the dialog style so
}
CMDtats(playerid, params[])
{
new tmp[128], string[512];
format(tmp, sizeof(tmp), "Money: $%d\n", GetPlayerMoney(playerid));
strcat(string, tmp);
format(tmp, sizeof(tmp), "Kills: %d\n", Player[playerid][Kills]);
strcat(string, tmp);
format(tmp, sizeof(tmp), "Deaths: %d\n", Player[playerid][Deaths]);
strcat(string, tmp);
format(tmp, sizeof(tmp), "Admin Level: %d\n", Player[playerid][Admin]);
ShowPlayerDialog(playerid, 6, DIALOG_STYLE_MSGBOX,""COL_LIGHTBLUE"Stats",string, "Ok","");
return 1;
}

Reply
#2

Error in syntax:
Код:
UPDATE `accounts` SET `Money` = '%d', ..., `Deaths` = '%d', WHERE `ID` = '%d'
Remove the comma before WHERE clause.

Always check mysql logs when something goes wrong with a query.
Reply
#3

thank you for helping.. But still it doesen't save my deaths/kills/money when I relogg :/
Reply
#4

in mysql_log it shows this, but I can't fix that
Quote:

[DEBUG] mysql_format - connection: 1, len: 512, format: "UPDATE `accounts` SET `Money` = '%d', `PosX` = '%f', `PosY` = '%f', `PosZ` = '%f', `PosA` = '%f', `Kills` = '%d', `Deaths` = '%d..."
[08:40:43] [ERROR] mysql_format - no value for specifier "%f" available
[08:40:43] [ERROR] mysql_format - no value for specifier "%f" available
[08:40:43] [ERROR] mysql_format - no value for specifier "%f" available
[08:40:43] [ERROR] mysql_format - no value for specifier "%d" available
[08:40:43] [ERROR] mysql_format - no value for specifier "%d" available
[08:40:43] [ERROR] mysql_format - no value for specifier "%d" available

Reply
#5

The count of placeholders does not match the count of arguments.

pawn Код:
mysql_format(mysql, query, sizeof(query),
    "UPDATE accounts \
        SET Money = %d, \
            PosX = %f, PosY = %f, PosZ = %f, PosA = %f, \
            Kills = %d, Deaths = %d \
      WHERE ID = %d"
,
GetPlayerMoney(playerid),
pos[0], pos[1], pos[2], pos[3],
Player[playerid][Kills], Player[playerid][Deaths],
Player[playerid][ID]);
This is wrong:
pawn Код:
mysql_tquery(mysql, query, "OnPlayerUpdate", "i", playerid);
There is already a public function under this name. Change it to something else or remove the specified callback altogether.

Use server-side money so cheats will not work.
Reply
#6

still not saving stats.. But ty
Reply
#7

try this
pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
     new query[800], 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', Kills='%d', \
     Deaths='%d' WHERE ID='%d'"
, Player[playerid][Money], pos[0], pos[1], pos[2], pos[3],
     Player[playerid][Kills], Player[playerid][Deaths], Player[playerid][ID]);
     mysql_tquery(mysql, query, "", "");
     return true;
}
You didn't provide Player ID in the query. Use server-side money so cheats will not work.
Example:
pawn Код:
stock GivePlayerCash(playerid, cash)
{
   Player[playerid][Money] += cash;
   GivePlayerMoney(playerid, cash);
}

CMD:cash(playerid, params[])
{
 GivePlayerCash(playerid, 282004);
}
Reply
#8

Quote:
Originally Posted by UFF
Посмотреть сообщение
You didn't provide Player ID in the query.
Thank you, I did not notice. Your code still has the comma before WHERE clause though.

Quote:
Originally Posted by robibacsika
Посмотреть сообщение
still not saving stats.. But ty
This is the reason I told you to always check mysql logs. It should inform you for my mistake:
pawn Код:
[ERROR] mysql_format - no value for specifier "%d" available
I will edit my previous post to add the last argument.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)