SQL Insert statement does not work in function but in Hook

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 does not work here



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


The SQL-Statement **does ** work here



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) 


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 serverside?

Have a wonderful day.

Here you go man, here’s a full page full of information regarding networking between server and client and the other way around.

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



net.Start("donation")
		net.WriteString(plyer:Nick())
		net.WriteString(plyer:SteamID())
		net.WriteString(paysafeCodeString)
	net.SendToServer()


serverside



net.Receive("donation", function(len, ply)
	print("Received the donation message!")
	print(net.ReadString()) -- name
	print(net.ReadString()) -- steamid
	print(net.ReadString()) -- paysafecode 
end)