26.12.2014, 13:19
Hello guys..i was learning how to use mysql recently and i've seen many tutorials..so i made a little register/login system using mysql. it compiled well but the player data are'nt saving. I would like if you give me a hand.
Here is the database table:
![](http://i.imgur.com/zgGY7GE.png)
And here is the script.
I'll appreciate your help guys and thanks in advance.
Here is the database table:
![](http://i.imgur.com/zgGY7GE.png)
And here is the script.
Код:
//MySQL Lessons Server #include <a_samp> #include <a_mysql> #include <sscanf2> #define SQL_HOST "localhost" #define SQL_USER "root" #define SQL_PASS "" #define SQL_DB "sa-mpsql" #define GREEN 0x33AA33FF #define RED 0xAA3333FF #define BLUE {0049FF} #define LOGIN1 0 #define LOGIN2 3 #define REGISTER1 1 #define REGISTER2 2 main() { print("\n----------------------------------"); print(" ** MySQL Lessons Server **"); print("----------------------------------\n"); } public OnGameModeInit() { // Don't use these lines if it's a filterscript SetGameModeText("MySQL Learning Server"); mysql_connect(SQL_HOST,SQL_USER,SQL_DB ,SQL_PASS); return 1; } public OnPlayerRequestClass(playerid, classid) { SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746); SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746); SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746); return 1; } public OnPlayerConnect(playerid) { new Query[80],pName[24],string[164]; GetPlayerName(playerid,pName,24); format(Query,sizeof(Query),"SELECT `Username` FROM `accounts` WHERE `Username` = '%s' LIMIT 1;",pName); mysql_query(Query); mysql_store_result(); if(mysql_num_rows() != 0) { format(string,sizeof(string),"Hey, %s! \nYour account is registered.\nPlease enter the password to log in!",pName); ShowPlayerDialog(playerid,0,DIALOG_STYLE_INPUT,"Login",string,"Login",""); } else { format(string,sizeof(string),"Hey, %s! \nYour account is not registered. \nPlease register to continue!",pName); ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Register",string,"Register",""); } mysql_free_result(); return 1; } //------------------[ENUMS]------------------- enum PlayerData { Username[23], Password[24], Money, Score, Float:PosX, Float:PosY, Float:PosZ } new PlayerInfo[MAX_PLAYERS][PlayerData]; //------------------[STOCKS]------------------ stock LoadStats(playerid) { new pName[24],Query[80]; GetPlayerName(playerid,pName,24); format(Query, sizeof(Query), "SELECT * FROM `accounts` WHERE `Username` = '%s' ", pName); mysql_query(Query); mysql_store_result(); mysql_fetch_row_format(Query, "|"); sscanf(Query, "e<p<|>s[24]s[23]i>", PlayerInfo[playerid]); mysql_free_result(); GivePlayerMoney(playerid,PlayerInfo[playerid][Money]); SetPlayerScore(playerid, PlayerInfo[playerid][Score]); SetPlayerPos(playerid, PlayerInfo[playerid][PosX],PlayerInfo[playerid][PosY],PlayerInfo[playerid][PosZ]); return 1; } stock LoginPlayer(playerid,const password[]) { new EscapedText[60]; new pName[24],Query[80]; GetPlayerName(playerid,pName,24); mysql_real_escape_string(password, EscapedText); format(Query,sizeof(Query),"SELECT * FROM `accounts` WHERE `Username` = '%s' AND `Password` = '%s'",pName ,EscapedText); mysql_query(Query); mysql_store_result(); if(mysql_num_rows() != 0) { SendClientMessage(playerid, GREEN ,"You have been logged in!"); LoadStats(playerid); } else { SendClientMessage(playerid, RED ,"Wrong password!"); Kick(playerid); } mysql_free_result(); return 1; } public OnPlayerDisconnect(playerid, reason) { new query[128], pName[24+1], Float:X, Float:Y, Float:Z; GetPlayerName(playerid, pName, sizeof(pName)); GetPlayerPos(playerid, X, Y, Z); format(query, sizeof(query), "UPDATE accounts SET score = %i, Money = %i, PosX = %f, PosY = %f, PosZ = %f WHERE Username LIKE %s;", GetPlayerScore(playerid), GetPlayerMoney(playerid), X, Y, Z, pName); mysql_query(query); return 1; } public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) { if(dialogid == 1) { if(strlen(inputtext) == 0) { ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose a password! \n","Register!",""); } else { new EscapedText[60],Query[80],pName[24]; GetPlayerName(playerid,pName,24); mysql_real_escape_string(inputtext, EscapedText); format(Query,sizeof(Query),"INSERT INTO `accounts` (Username,Password,Money) VALUES ('%s','%s','0')",pName,EscapedText); mysql_query(Query); SendClientMessage(playerid,GREEN,"You have been successfully registered!"); GivePlayerMoney(playerid,5000); } } if(dialogid == 0) { if(strlen(inputtext) == 0) { ShowPlayerDialog(playerid,3,DIALOG_STYLE_INPUT,"Login - Enter your password","You need to insert your password","Login",""); } else { LoginPlayer(playerid,inputtext); } } return 1; }