AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   SQL Insert and Update Player Info (https://forums.alliedmods.net/showthread.php?t=320561)

xRENN1Ex 12-28-2019 03:17

SQL Insert and Update Player Info
 
1 Attachment(s)
Player info is registering MYSQL to update, but does not update. Can you help me?

(ip, name, authid, hp, alive or dead

Code:

new Handle:Query = SQL_PrepareQuery(SqlConnection, "INSERT INTO `playerlogger` (`ip`, `name`, `authid`, `hp`, `df_canli`) VALUES  ('%s','%s','%s','%i','%i') ON DUPLICATE KEY UPDATE `ip`=VALUES(`ip`), `name`=VALUES(`name`), `authid`=VALUES(`authid`), `hp`=VALUES(`hp`), `df_canli`=VALUES(`df_canli`);", szIP, szName, szAuthid, iL_HP, df_canli);

Natsheh 12-28-2019 06:51

Re: SQL Insert and Update Player Info
 
Do debugging and see where your code fails.

^SmileY 12-28-2019 07:16

Re: SQL Insert and Update Player Info
 
This query is totally wrong. Why you supposed to updated inserted record?

xRENN1Ex 12-28-2019 08:05

Re: SQL Insert and Update Player Info
 
Can you do the right thing?
Because players want to reflect the view. You can look here as an example

https://www.csduragi.com/monitor/cs8.csduragi.net

Natsheh 12-28-2019 10:54

Re: SQL Insert and Update Player Info
 
Use replace instead .

xRENN1Ex 12-28-2019 11:37

Re: SQL Insert and Update Player Info
 
Quote:

Originally Posted by Natsheh (Post 2678272)
Use replace instead .



can you give me information about how to use it?

Bugsy 12-28-2019 13:37

Re: SQL Insert and Update Player Info
 
You need to include a primary key in the statement otherwise it will work the same as insert. If any fields in the record have a value then you must include them in the statement, even if they are not being changed, otherwise they will be set to null. In the below example my primary key in the table is ID, set to auto-increment to make inserting new records easy.

Suppose I have the below record:

ID = 223
SteamID = STEAM_0:1:12345
PlayerName= bugsy
PlayTime= 555

I can then do the below to update PlayTime to 123. Fields SteamID & PlayerName will remain unchanged since I am including the existing values in the statement.

Code:

REPLACE INTO tblPlayerData (ID, SteamID, PlayerName, PlayTime) VALUES (223,'STEAM_0:0:12345','bugsy',123);
If I also have a column XPPoints in the table, it would be set to null with the above REPLACE INTO statement, unless I specify a value.

Assume these fields exist in a row:

ID = 223
SteamID = STEAM_0:1:12345
PlayerName= bugsy
PlayTime= 555
XPPoints = 555

Code:

REPLACE INTO tblPlayerData (ID, SteamID, PlayerName, PlayTime, XPPoints) VALUES (223,'STEAM_0:0:12345','bugsy',123,555);
This will retain XPPoints value of 555 (and leave SteamID & PlayerName as is), while changing PlayTime to 123.

When adding a new record, just exclude the ID field and it will add a new record, advancing ID to the next slot automatically.

Code:

REPLACE INTO tblPlayerData (SteamID, PlayerName) VALUES ('STEAM_0:0:555','bugsy2');

xRENN1Ex 12-28-2019 15:13

Re: SQL Insert and Update Player Info
 
Quote:

Originally Posted by Bugsy (Post 2678295)
You need to include a primary key in the statement otherwise it will work the same as insert. If any fields in the record have a value then you must include them in the statement, even if they are not being changed, otherwise they will be set to null. In the below example my primary key in the table is ID, set to auto-increment to make inserting new records easy.

Suppose I have the below record:

ID = 223
SteamID = STEAM_0:1:12345
PlayerName= bugsy
PlayTime= 555

I can then do the below to update PlayTime to 123. Fields SteamID & PlayerName will remain unchanged since I am including the existing values in the statement.

Code:

REPLACE INTO tblPlayerData (ID, SteamID, PlayerName, PlayTime) VALUES (223,'STEAM_0:0:12345','bugsy',123);
If I also have a column XPPoints in the table, it would be set to null with the above REPLACE INTO statement, unless I specify a value.

Assume these fields exist in a row:

ID = 223
SteamID = STEAM_0:1:12345
PlayerName= bugsy
PlayTime= 555
XPPoints = 555

Code:

REPLACE INTO tblPlayerData (ID, SteamID, PlayerName, PlayTime, XPPoints) VALUES (223,'STEAM_0:0:12345','bugsy',123,555);
This will retain XPPoints value of 555 (and leave SteamID & PlayerName as is), while changing PlayTime to 123.

When adding a new record, just exclude the ID field and it will add a new record, advancing ID to the next slot automatically.

Code:

REPLACE INTO tblPlayerData (SteamID, PlayerName) VALUES ('STEAM_0:0:555','bugsy2');



thank you for the answer

so can you ask me to do this sma coding

I don't have many resources because I am Turkish so I will be forced

I need help because it has a different structure than php

xRENN1Ex 12-28-2019 16:59

Re: SQL Insert and Update Player Info
 
Code:

public RegisterUser(id){
    new szIP[32],szName[32],szAuthid[32],h_Healts,a_Alive;
    get_user_ip(id,szIP,charsmax(szIP), 1)
    get_user_name(id,szName,charsmax(szName))
    get_user_authid(id,szAuthid,charsmax(szAuthid))
    h_Healts = get_user_health(id)
    a_Alive = is_user_alive(id)
   
    new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
    if(SqlConnection == Empty_Handle)
        set_fail_state(g_Error)

          new Handle:Query = SQL_PrepareQuery(SqlConnection, "INSERT INTO `playerlogger` (`ip`, `name`, `authid`, `h_Healts`, `a_Alive`) VALUES  ('%s','%s','%s','%i','%i') ON DUPLICATE KEY UPDATE `playerlogger` SET `h_Healts` = REPLACE(`h_Healts`, `h_Healts`, `99`);", szIP, szName, szAuthid, h_Healts, a_Alive);
   

    if(!SQL_Execute(Query)){
        SQL_QueryError(Query,g_Error,511)
        set_fail_state(g_Error)
    }
   
    SQL_FreeHandle(Query)
    SQL_FreeHandle(SqlConnection)
}

is this code block correct?


All times are GMT -4. The time now is 07:52.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.