SA-MP Forums Archive
MySql won't insert into database? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: MySql won't insert into database? (/showthread.php?tid=584392)



MySql won't insert into database? - LegendOfScripts - 04.08.2015

So, When a player registers it shows the dialog twice and the first time it says the account isn't registered but when he see's the second one and clicks it says this account already exists.

Code:
public OnPlayerRegister(playerid, password[])
{
	new query[240];
	if(IsPlayerConnected(playerid))
	{
	    if(GetPVarInt(playerid, "AccountExist") == 0)
	    {
	        new hashpass[129];
	    	WP_Hash(hashpass, sizeof(hashpass), password);
	    	format(query, sizeof(query), "INSERT INTO accounts (Name,Pass) VALUES ('%s', '%s')", PlayerInfo[playerid][pUsername], password);
	        mysql_function_query(handlesql, query, false, "SendQuery", "");
	        //==========//
	        SetPVarInt(playerid, "Cash", 500);
		    SetPVarInt(playerid, "Bank", 0);
		    SetPVarInt(playerid, "Model", 26);
		    SetPVarInt(playerid, "Interior", 0);
		    SetPVarInt(playerid, "World", 0);
		    SetPVarInt(playerid, "Tut", 0);
		    SetPVarInt(playerid, "Age", 14);
		    SetPVarInt(playerid, "Sex", 1);
		    SetPVarFloat(playerid, "PosX", 1642.7285);
		    SetPVarFloat(playerid, "PosY", -2240.5591);
		    SetPVarFloat(playerid, "PosZ", 13.4945);
		    SetPVarFloat(playerid, "Health", 50.0);
		    SetPVarFloat(playerid, "Armour", 0.0);
	        //==========//
	        SetPVarInt(playerid, "AccountExist", 1);
	        CheckAccount(playerid, 1);
	    }
	    else KickPlayer(playerid, "Unable to register, account exists!");
	}
	return 1;
}

stock CheckAccount(playerid, type)
{
	printf("CheckAccount is called [ID %i]", playerid);
    new query[82];
	mysql_format(handlesql, query, sizeof(query), "SELECT * FROM `accounts` WHERE `Name` = '%e'", PlayerInfo[playerid][pUsername]);
	print("Send a query.");
	mysql_pquery(handlesql, query, "OnAccountCheck", "dd", playerid, type);
	print("Qury sent to OnAccountCheck.");
	return 1;
}

public OnAccountCheck(playerid, type)
{
	printf("OnAccountCheck is called [ID %i]", playerid);
	if(playerid != INVALID_PLAYER_ID)
	{
		new rows, fields;
		cache_get_data(rows, fields, handlesql);
		if(rows)
		{
			new fetch[24], query[256];
			cache_get_field_content(0, "ConnectTime", fetch);

			mysql_format(handlesql, query, sizeof(query), "SELECT `deleted` FROM `accounts` WHERE `Name` = '%s'", PlayerInfo[playerid][pUsername]);
			mysql_pquery(handlesql, query, "OnDeletedCheck", "d", playerid);
			
			//==========//
		    cache_get_field_content(0, "Tut", fetch);
		    SetPVarInt(playerid, "Tut", strval(fetch));
		    SetPVarInt(playerid, "AccountExist", 1);
		    if(type == 1)
		    {
  				SetPVarInt(playerid, "AccountExist", 1);
  				cache_get_row(0,2,PlayerInfo[playerid][pPass],handlesql, 128);
	            ShowPlayerDialog(playerid,1,DIALOG_STYLE_PASSWORD,"Server Account","An existing account is using your playername, please login to the account!","Login", "");
	            CheckIfBanned(playerid);
	        }
		}
		else
		{
		    ShowPlayerDialog(playerid,2,DIALOG_STYLE_PASSWORD,"Server Account","There are no existing account using your playername, please create a new account!","Register", "");
		}
	}
	return 1;
}
I want it show the register dialog then the login dialog afterwards but it continues to show the register dialog.


Re: MySql won't insert into database? - LegendOfScripts - 04.08.2015

please helps


Re: MySql won't insert into database? - Vince - 04.08.2015

Are you familiar with the term "race condition"? It is possible that you're querying the database before it's done inserting the new data. Besides, having a player log in after they just registered is rather dumb. Just log them in at once.


Re: MySql won't insert into database? - LegendOfScripts - 04.08.2015

Quote:
Originally Posted by Vince
View Post
Are you familiar with the term "race condition"? It is possible that you're querying the database before it's done inserting the new data. Besides, having a player log in after they just registered is rather dumb. Just log them in at once.
did it with just loading still same thing, I log out and go to log in, Player doesn't exist.


Re: MySql won't insert into database? - LegendOfScripts - 04.08.2015

Anyone help it will not save the Username or Password into the database.