Store the name, steamid and IP on the database when he joins the server. But, when connecting I want to the plugin to check if the player is already registered. So, we get the IP that is stored on the database for the player's steamid. If the database player's IP does not match to the actual player's IP, I want it to write a new line with new data, as such the name, steamID and iP.
if(StrEqual(dbIP, iip))
{
PrintToServer("[DB] Player is already saved on the database.");
return;
}
char newquery[256];
Format(newquery, sizeof(newquery), "INSERT INTO players (name, steamid, ip) VALUES ('%s', '%s', '%s')", iname, iid, iip);
DBResultSet query2 = SQL_Query(DB, newquery);
if(query2 == null)
{
PrintToServer("[DB] Could not save new player data.");
return;
}
PrintToServer("[DB] New player data was save in the database with success.");
return;
}
Any solution for the plugin to fix my issue?
OBS: That plugin was storing the player data EVERYTIME he joined the server, so I tried to update it to a more NON-SPAMMING version. This "new update" on the source above is creating the tables but not storing data.
Your sql query is always inserting
You should update instead of insert if the player is already in the databse
And you should definetly use threaded query
Your sql query is always inserting
You should update instead of insert if the player is already in the databse
And you should definetly use threaded query
Hey! Thanks for your reply. I did some changes and now it is storing the values but keeps storing everytime I join the server. Any idea to just store when he connects for the first time and when he changes the IP?
if(StrEqual(dbip, iip))
{
PrintToServer("[DB] Player is already in the Database. Skipping...");
return;
}
else
{
char StoreAgain[256];
Format(StoreAgain, sizeof(StoreAgain), "INSERT INTO players (name, steamid, ip) VALUES ('%s', '%s', '%s')", iname, iid, iip);
DBResultSet NewStore = SQL_Query(DB, StoreAgain);
if(NewStore == null)
{
PrintToServer("[DB] Could not insert new data to the database.");
return;
}
else
{
PrintToServer("[DB] New player data was stored with success on the database.");
return;
}
}
}
else
{
char newquery[256];
Format(newquery, sizeof(newquery), "INSERT INTO players (name, steamid, ip) VALUES ('%s', '%s', '%s')", iname, iid, iip);
DBResultSet query2 = SQL_Query(DB, newquery);
if(query2 == null)
{
PrintToServer("[DB] Could not save new player data.");
return;
}
else
{
PrintToServer("[DB] New player data was save in the database with success.");
return;
}
}
}
Hey! Thanks for your reply. I did some changes and now it is storing the values but keeps storing everytime I join the server. Any idea to just store when he connects for the first time and when he changes the IP?
if(StrEqual(dbip, iip))
{
PrintToServer("[DB] Player is already in the Database. Skipping...");
return;
}
else
{
char StoreAgain[256];
Format(StoreAgain, sizeof(StoreAgain), "INSERT INTO players (name, steamid, ip) VALUES ('%s', '%s', '%s')", iname, iid, iip);
DBResultSet NewStore = SQL_Query(DB, StoreAgain);
if(NewStore == null)
{
PrintToServer("[DB] Could not insert new data to the database.");
return;
}
else
{
PrintToServer("[DB] New player data was stored with success on the database.");
return;
}
}
}
else
{
char newquery[256];
Format(newquery, sizeof(newquery), "INSERT INTO players (name, steamid, ip) VALUES ('%s', '%s', '%s')", iname, iid, iip);
DBResultSet query2 = SQL_Query(DB, newquery);
if(query2 == null)
{
PrintToServer("[DB] Could not save new player data.");
return;
}
else
{
PrintToServer("[DB] New player data was save in the database with success.");
return;
}
}
}
- This create table, which set timestamp on every record.
- When query player IP from database, plugin fetch first row only. Because we order query results by timestamp DESC.
- Plugin compare player IP with database IP. If not match, it makes new record.
PHP Code:
Handle MyDataBase;
public void OnPluginStart()
{
SQL_TConnect(connect_callback, "test");
}
public void connect_callback(Handle owner, Handle hndl, const char[] error, any data)
{
MyDataBase = hndl;
Format(query, sizeof(query), "SELECT `ip` FROM `players` WHERE `steamid`='%s' ORDER BY `players`.`timestamp` DESC", auth);
SQL_TQuery(MyDataBase, query_callback, query, GetClientUserId(client));
}
public void query_callback(Handle owner, Handle hndl, const char[] error, any data)
{
int client = GetClientOfUserId(data);
public void record_callback(Handle owner, Handle hndl, const char[] error, any data)
{
if(hndl == null)
{
LogError("record Error: %s", error);
// you going to see "... record Error: UNIQUE constraint failed: players.steamid, players.ip" when save duplicate steamid+IP record
return;
}
PrintToServer("success");
}
This will record different steamid+ip, no duplicates.
...unless, you want log history of ip changes, I think you need either to use timestamp to look latest IP record, or look bigest row ID number
*edit
Updated previous post