• SQL Insert statement does not work in function but in Hook
    5 replies, posted
Dear forum, I am trying to execute an SQL-Statement (integrated sql library) in a "DoClick" function (DButton), but it does not work. However, if I use the same SQL-Statement in a random function/hook outside the "DoClick" function, then it does work. I have no clue for a reason for this. Does anybody have an idea? Here's the code: The SQL-Statement [B]does not[/B] work here [CODE] DonateButton.DoClick = function() -- DoClick function paysafeCodeString = psc1:GetValue() .. psc2:GetValue() .. psc3:GetValue() .. psc4:GetValue() plyer:ChatPrint("works right now...") sql.Query("INSERT INTO donations (PlayerName, SteamID, Paysafecode) VALUES ('what', 'the', 'fck');") --result = sql.LastError() --plyer:ChatPrint(result) -- this prints out "column PlayerName is not unique" end [/CODE] The SQL-Statement [B]does [/B] work here [CODE] hook.Add('PlayerSay', 'ChatCommand', function(ply, text) -- PlayerSay hook if string.len(DonationSystem.OpenCommand) > 0 then if string.sub(text, 0, string.len(DonationSystem.OpenCommand)) == DonationSystem.OpenCommand then sql.Query("INSERT INTO donations (PlayerName, SteamID, Paysafecode) VALUES ('what', 'the', 'fck');") -- the sql statement did work: what | the | fck was inserted. ply:ConCommand("OpenDonationMenu") return '' end end end) [/CODE]
Thats because the first one is executing clientside, you'll need to use the net library to send a message from the client to the server and run the query serverside.
Thank you in advance for the quick response. I'm wondering how the thing works. I know it has to do with net.Receive or something, but I'm not quite sure. May I ask you to give me an example of how to send the query [B]serverside[/B]? Have a wonderful day.
[QUOTE=_TheJok3r_;48938618]Thank you in advance for the quick response. I'm wondering how the thing works. I know it has to do with net.Receive or something, but I'm not quite sure. May I ask you to give me an example of how to send the query [B]serverside[/B]? Have a wonderful day.[/QUOTE] Here you go man, here's a full page full of information regarding networking between server and client and the other way around. [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
Read about the difference between clientside and serverside You need to send a net message to the server saying he clicked the button and do your query there
Thanks for all the responses. I fixed the problem with a simple method: clientside [CODE] net.Start("donation") net.WriteString(plyer:Nick()) net.WriteString(plyer:SteamID()) net.WriteString(paysafeCodeString) net.SendToServer() [/CODE] serverside [CODE] net.Receive("donation", function(len, ply) print("Received the donation message!") print(net.ReadString()) -- name print(net.ReadString()) -- steamid print(net.ReadString()) -- paysafecode end) [/CODE]
Sorry, you need to Log In to post a reply to this thread.