Raised This Month: $ Target: $400
 0% 

[Help] MySQL & PHP


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
Croxeye
Member
Join Date: Jul 2015
Old 01-30-2016 , 06:25   [Help] MySQL & PHP
Reply With Quote #1

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(PLUGINVERSIONAUTHOR)
    
    
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(idSteamIDcharsmax(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"ErrcodeError)
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError)
    }

    new 
id
    id 
Data[0]
    
    if(
SQL_NumResults(Query) < 1
    {
        new 
SteamID[32]
        
get_user_authid(idSteamIDcharsmax(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(Query1)
    }
    
    return 
PLUGIN_HANDLED
}

public 
Save_MySql(id)
{
    new 
SteamID[32], szTemp[512]
    
get_user_authid(idSteamIDcharsmax(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)
{    
    
ColorChatidGREEN"^1[^4GOLD SYSTEM^1] You Have ^4%d ^1Golds"gGoldsid ] );    
}

public 
Hook_Deathmessage()
{
    new 
killer read_data);
    new 
victim read_data);
    
    if( 
killer == victim )
    {
        return 
PLUGIN_HANDLED;
    }
    
    
gGoldskiller ] += 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)

Last edited by Croxeye; 01-30-2016 at 06:29.
Croxeye is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:25.


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