This is simple code, 1 KILL = 1 GOLD saved in MySQL database
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <ColorChat>
#include <sqlx>
#define PLUGIN "MySQL Gold System"
#define VERSION "1.0"
#define AUTHOR "Croxeye"
new Host[] = "localhost"
new User[] = "root"
new Pass[] = ""
new Db[] = "server"
new Handle:g_SqlTuple
new g_Error[512]
new gGolds [ 33 ];
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event( "DeathMsg", "Hook_Deathmessage", "a" );
register_clcmd( "say /golds", "cmd_showgolds" );
set_task(1.0, "MySql_Init")
}
public MySql_Init()
{
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(SqlConnection == Empty_Handle)
set_fail_state(g_Error)
new Handle:Queries
Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS save_golds (SteamID varchar(32), Golds INT(11))")
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
SQL_FreeHandle(Queries)
SQL_FreeHandle(SqlConnection)
}
public client_putinserver(id)
{
Load_MySql(id)
}
public client_disconnect(id)
{
Save_MySql(id)
}
public plugin_end()
{
SQL_FreeHandle(g_SqlTuple)
}
public Load_MySql(id)
{
new SteamID[32], szTemp[512]
get_user_authid(id, SteamID, charsmax(SteamID))
new Data[1]
Data[0] = id
format(szTemp,charsmax(szTemp),"SELECT * FROM `save_golds` WHERE (`save_golds`.`SteamID` = '%s')", SteamID)
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
}
public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
{
log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
}
else if(FailState == TQUERY_QUERY_FAILED)
{
log_amx("Load Query failed. [%d] %s", Errcode, Error)
}
new id
id = Data[0]
if(SQL_NumResults(Query) < 1)
{
new SteamID[32]
get_user_authid(id, SteamID, charsmax(SteamID))
if (equal(SteamID,"ID_PENDING"))
return PLUGIN_HANDLED;
new szTemp[512]
format(szTemp,charsmax(szTemp),"INSERT INTO `save_golds` ( `SteamID` , `Golds`)VALUES ('%s','0');", SteamID)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
else
{
gGolds[id] = SQL_ReadResult(Query, 1)
}
return PLUGIN_HANDLED
}
public Save_MySql(id)
{
new SteamID[32], szTemp[512]
get_user_authid(id, SteamID, charsmax(SteamID))
format(szTemp,charsmax(szTemp),"UPDATE `save_golds` SET `Golds` = '%i' WHERE `save_golds`.`SteamID` = '%s';", gGolds[id], SteamID)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
SQL_FreeHandle(Query)
return PLUGIN_HANDLED
}
public cmd_showgolds(id)
{
ColorChat( id, GREEN, "^1[^4GOLD SYSTEM^1] You Have ^4%d ^1Golds", gGolds[ id ] );
}
public Hook_Deathmessage()
{
new killer = read_data( 1 );
new victim = read_data( 2 );
if( killer == victim )
{
return PLUGIN_HANDLED;
}
gGolds[ killer ] += 1
return PLUGIN_CONTINUE;
}
So, I created a PHP page to add golds using MySQL
PHP Code:
<html>
<head>
<title>MySQL Gold System</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form method="POST" action="golds_send.php">
<br><label for="steamid"><strong>SteamID</strong></label>
<br><input type="text" id="steamid" name="steamid" required="required">
<br><label for="golds"><strong>Golds</strong></label>
<br><input type="number" id="golds" name="golds" required="required">
<br><br><input type="submit" name="submit" id="submit" value="Send">
</form>
</body>
<html>
PHP Code:
<?php
$connect = new PDO('mysql:host=localhost;dbname=server', 'root', '');
$steamid = $_POST['steamid'];
$golds = $_POST['golds'];
if ($steamid&&$golds) {
$connect->query("UPDATE `save_golds` SET `golds`= `golds` + '$golds' WHERE `save_golds`.`steamid` = '$steamid'");
}
?>
When I add golds to player from php page and then I change map, golds that I added from page are removed. (Ex. I killed 10 player and I get 10 golds. I gave myself 100 golds from php page and when I check database, I see 110 golds but when I restart server or change map, My golds are set to 10 again)