Save player data to mysql
Hello all. I have simple SQL request from server to save player data into mysql. Like kills, deaths, steam_id and etc. Then player join in the server, made some kills SQL not update existing table row, but creating a new one without steam id, nickname and etc... Whats wrong? My code:
PHP Code:
|
Re: Save player data to mysql
Use update instead of replace.
https://www.w3schools.com/sql/sql_update.asp Also, you do not need to specify all columns if you set values in correct order, and format with %i for integers instead of %d. Code:
More info about sql: https://forums.alliedmods.net/showth...p?t=172936#SQL |
Re: Save player data to mysql
My bad crazy, you're right. As long as all fields are supplied and in the correct order, your method will work. I've never personally used INSERT without specifying field names and I literally write SQL at work almost every day. Still, I think it's good practice to include field names, but thats just my opinion. jonatat, when a player connects then use a query to retrieve their data and store it in an array. On disconnect, if data exists in the array, use an UPDATE statement. If the array is null, use an INSERT statement. Code:
INSERT INTO tblTest (SteamID, Val1, Val2) VALUES ('STEAM:0:12345',12,34); |
Re: Save player data to mysql
You can also use REPLACE INTO if your table is created properly.
Code:
REPLACE works exactly like INSERT , except that if an old row in the table has Code:
CREATE TABLE IF NOT EXISTS tblPlayerData (SteamID VARCHAR(34) PRIMARY KEY , PlayerName VARCHAR(32), PlayTime INTEGER); |
Re: Save player data to mysql
@Bugsy, yes, this is a prefer of each programmer, both will work. I did not found results about REPLACE INTO in w3c, only about replace strings. Are you sure that this will work?
|
Re: Save player data to mysql
Quote:
|
Re: Save player data to mysql
Thanks guys for helping me out! Can i use UPDATE instead of REPLACE? Becouse i'm using same users table in server and website
|
Re: Save player data to mysql
Quote:
Does it okay? Code:
formatex( szQuery, 3799, "UPDATE `users` SET `steam_id` = '%s', `player_name` = '%s', `kills` = '%d', `deaths` = '%d', `headshots` = '%d', `aces` = '%d', `m_aces` = '%d', `mix_played` = '%d', `mix_lost` = '%d', `mix_won` = '%d', `mix_draw` = '%d', `points` = '%d' WHERE `steam_id` = '%s';", steam, GetSecureName(Name), pKills[id], pDeaths[id], pHeadshots[id], pAces[id], pMiniAces[id], pMixPlayed[id], pMixLost[id], pMixWon[id], pMixDraw[id], pPoints[id], steam); |
Re: Save player data to mysql
Yes, you can. Just do the syntax correctly and will work.
Quote:
|
Re: Save player data to mysql
You don't need to update the steamid value
|
| All times are GMT -4. The time now is 04:43. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.