12.02.2018, 15:25
Hello there SA-MP community, and thank you for clicking my thread! As the title says, INSERT INTO is not working properly. Let me give you more details. As you can see below, there is a stock that inserts the player's record, and a stock that loads player data from that record. (Registering the player more specifically and loading it's data).
This is the register_user() stock:
This is the load_playerdata() stock:
Excuse me if the code is kinda confusing or noobish, I just want to register/load/save data with stocks, and I believe it is possible. (Or is it?). Anyways, so why INSERT INTO is not working?
I have tried:
This is the register_user() stock:
PHP код:
forward register_user( playerid, passwd[], bool: password );
stock register_user( playerid, passwd[], bool:password )
{
print("SERVER: Okay Chris, 'register_user()' was called.\n");
new
pName[MAX_PLAYER_NAME],
Ip[16],
Query1[512],
Query2[256/2],
DBResult: Result1
;
print("SERVER: Variables registered.\n");
GetPlayerName( playerid, pName, sizeof( pName ) );
printf("SERVER: I tried to get the playerid's name. (It returned '%s')\n", pName);
GetPlayerIp( playerid, Ip, sizeof( Ip ) );
printf("SERVER: Cool, now I got the IP of the playerid. (It returned '%s'. I am also going to get the date!)\n", Ip);
getdate( Year, Month, Day );
print("SERVER: Now we are into the password boolean check-up, let me see what it will return!\n");
if(password)
{
print("SERVER: Looks like there is an actual password, I am going to format the query now. If I don't say 'Test', that means that I failed in formatting it!\n");
format( Query1, sizeof( Query1 ), "INSERT INTO `Users` (Username, Password, IP, Job, Skin, Money, Health, Armor, AdminLevel) VALUES ('%s', '%s', '%s', 'undefined', '0', '25000', '100.0000', '0.0000', '0');", pName, DB_Escape( passwd ), Ip);
print("SERVER: Test! Looks like 'format()' ran good.\n");
}
else
{
print("SERVER: NO PASSWORD DETECTED, I am going to format the query now. If I don't say 'Test', that means that I failed in formatting it!\n");
format( Query1, sizeof( Query1 ), "INSERT INTO `Users` (Username, Password, IP, Job, Skin, Money, Health, Armor, AdminLevel) VALUES ('%s', '%s', '%s', 'undefined', '0', '25000', '100.0000', '0.0000', '0'');", pName, "null", Ip);
print("SERVER: Test! Looks like 'format()' ran good.\n");
}
print("SERVER: Okayyy... so I formatted the passwords, null or not. Now I am going to run the query!\n");
db_query( Database, Query1 );
print("SERVER: Ran the query, I am going to try to reset the 'Query1' variable to '0' now.\n");
format(Query1, sizeof(Query1), "");
print("SERVER: Good, now time to put the inputtext as password in the enum thingy.\n");
format( PlayerData[playerid][Password], 65, passwd );
print("SERVER: Okay, now I am going to tell 'load_playerdata()' to do it's job!\n");
load_playerdata( playerid );
print("SERVER: It looks like 'load_playerdata()' did it's job!\n");
print("SERVER: If this message is still shown up then WOW! We reached at the 570th line of the script, it's time to change XYZAngle values to a random airport.\n");
new rand = random( sizeof Spawnpoints );
print("SERVER: I made the 'rand' variable.");
PlayerData[playerid][X] = Spawnpoints[rand][0];
PlayerData[playerid][Y] = Spawnpoints[rand][1];
PlayerData[playerid][Z] = Spawnpoints[rand][2];
PlayerData[playerid][Angle] = Spawnpoints[rand][3];
print("SERVER: Looks good, now it's time to announce to the player that he got registered in the fricking database! xD\n");
SendClientMessage( playerid, -1, "I registered you in the fricking database now!\n" );
print("SERVER: Told him it.\n");
print("SERVER: Running another Query(2) to get the amount of records in the database, so we can tell the players registered number!\n");
format( Query2, sizeof Query2, "SELECT * FROM `Users`;" );
Result1 = db_query( Database, Query2 );
print("SERVER: It's time to tell everyone in the server that someone joined!\n");
new str[256];
format( str, sizeof( str ), "{A8C7FF}SERVER:{FFFFFF} Welcome {F9EC00}%s{FFFFFF} to the server! We now have %d players registered!", pName, db_num_rows( Result1 ) );
db_free_result( Result1 );
SendClientMessageToAll( -1, str );
print("\nSERVER: My job is done here!\n");
}
PHP код:
stock load_playerdata( playerid )
{
new
Query[256],
DBResult: Result,
pName[MAX_PLAYER_NAME]
;
GetPlayerName( playerid, pName, sizeof pName );
format( Query, sizeof Query, "SELECT * FROM `Users` WHERE `Username` = '%s';", pName );
Result = db_query( Database, Query );
PlayerData[playerid][Skin] = db_get_field_assoc_int( Result, "Skin" );
PlayerData[playerid][X] = db_get_field_assoc_float( Result, "X" );
PlayerData[playerid][Y] = db_get_field_assoc_float( Result, "Y" );
PlayerData[playerid][Z] = db_get_field_assoc_float( Result, "Z" );
PlayerData[playerid][Angle] = db_get_field_assoc_float( Result, "Angle" );
PlayerData[playerid][Interior] = db_get_field_assoc_int( Result, "Interior" );
PlayerData[playerid][Money] = db_get_field_assoc_int( Result, "Money" );
PlayerData[playerid][Health] = db_get_field_assoc_float( Result, "Health" ); SetPlayerHealth( playerid, PlayerData[playerid][Health] );
PlayerData[playerid][Armor] = db_get_field_assoc_float( Result, "Armor" ); SetPlayerArmour( playerid, PlayerData[playerid][Armor] );
PlayerData[playerid][AdminLevel] = db_get_field_assoc_int( Result, "AdminLevel" );
db_get_field_assoc( Result, "Username", PlayerData[playerid][Username], 26 );
db_get_field_assoc( Result, "IP", PlayerData[playerid][IP], 16+1 );
format( PlayerData[playerid][Job], 32, "undefined" );
db_free_result( Result );
}
I have tried:
- Executing the query from my SQL Browser (DB Browser for SQLite), it DID work there!
- Removing the 'if( password )', didn't work.