SQL Set a New Name

Hello all, and merry Christmas.

I am currently writing my own Gamemode for Garry’s Mod, I didn’t have any prior knowledge to LUA as of 2 days ago, so I’m sort of learning as I go. I am having trouble with updating an SQL table row.

I have a small function that checks if someone has visited the server before, by creating a new entry in the SQL table and then checking if their SteamID is listed in one of the rows. I then have a small function that draws a DTextEntry and a DButton in a DFrame, the “new user” can then enter a name in the box and it will update the the SQL table getting the value from the DTextEntry.

The problem I am having is that when they click the button, their name doesn’t get updated in the table. There is a line of code I added that tells the console what the data in the entry field was so prior to sending the usermessage so I know it’s not that.

Here is my sv_sql.lua file. I think it may be re-updating the row somewhere in the code but I couldn’t make heads or tails about it. Anybody have any feedback?


function tables_exist()
	if (sql.TableExists("player_info")) then
		Msg("Table: 'player_info' already exists
")
	else
		if (!sql.TableExists("player_info")) then
			query = "CREATE TABLE player_info (id varchar(255), name varchar(20), money int)"
			result = sql.Query(query)
			
			if (sql.TableExists("player_info")) then
				Msg("Table: 'player_info' has been created
")
			else
				Msg("Error: 'player_info' could not be created
")
				Msg(sql.LastError(result).."
")
			end
		end
	end
end

function Initialize()
	tables_exist()
end

hook.Add("Initialize", "Initialize", Initialize)

function PlayerInitialSpawn(ply)
	timer.Create("Steam_ID_Delay", 5, 0, function()
		SteamID = ply:SteamID()
		ply:SetNWString("SteamID", SteamID)
		
		// timer.Create("SaveStat", 10, 0, function() saveStat(ply) end) // Remove this line later on when stats stuff is added
		
		timer.Create("TimedMoney", 5, 0, function() timedMoney(ply) end) // Will make a function of this soon
		
		player_exists(ply)
	end)
end

hook.Add("PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn)

function player_exists(ply)
	SteamID = ply:GetNWString("SteamID")
	
	result = sql.Query("SELECT id, name, money FROM player_info WHERE id = '"..SteamID.."'")
	
	if (result) then
		sql_value_stats(ply)
	else
		new_player(SteamID, ply)
	end
end

function new_player(SteamID, ply)
	SteamID = SteamID
	
	sql.Query("INSERT INTO player_info ('id', 'name', 'money') VALUES('"..SteamID.."', 'Unknown', '50')")
	result = sql.Query("SELECT id, name, money FROM player_info WHERE id = '"..SteamID.."'")
	
	if (result) then
		Msg("Table: New player added to the table
")
	else
		Msg("Error: New player could not be created
")
	end
end

function sql_value_stats(ply)
	id = sql.QueryValue("SELECT id FROM player_info WHERE id = '"..SteamID.."'")
	name = sql.QueryValue("SELECT name FROM player_info WHERE id = '"..SteamID.."'")
	money = sql.QueryValue("SELECT money FROM player_info WHERE id = '"..SteamID.."'")
	
	ply:SetNWString("id", id)
	ply:SetNWString("name", name)
	ply:SetNWString("money", money)
end

function saveStat(ply)
	name = ply:GetNWString("name")
	money = ply:GetNWInt("money")
	
	sql.Query("UPDATE player_info SET name = "..name..", money = "..money.." WHERE id = '"..SteamID.."'")
	
	ply:ChatPrint("[BradScript] Your player info has been updated!")
end

function timedMoney(ply)
	money = ply:GetNWInt("money")
	
	newmoney = money + 50
	
	ply:SetNWInt("money", newmoney)
	
	sql.Query("UPDATE player_info SET money = "..newmoney.." WHERE id = '"..SteamID.."'")
	
	ply:ChatPrint("[BradScript] You just earned 50 pounds for playing on the server a while!")
end

function newplayer_setname(ply, command, arguments)
	SteamID = ply:GetNWString("SteamID")
	newname = arguments[1]
	
	sql.Query("UPDATE player_info SET name = "..newname.." WHERE id = '"..SteamID.."'")
	ply:ChatPrint("[BradScript] Your players name is now, "..newname.."!")
	
	ply:SetNWString("name", newname)
end

concommand.Add("bs_newplayer_setname", newplayer_setname)

Oh and PS. I know it’s messy, as I said I’m learning as I go, I’m going to clean the code up a bit once I know it’s working.


(User was banned for this post ("Wrong section" - mahalis))

-snip- misread, although it looks pretty good for being 2 days into coding.

To make sure the query is correct, let it print to the console. Your condition in the update query might not match any of the entries in the table (i.e bad steamid).

put quotes around the name that is being inserted otherwise a space could cause a SQL Syntax Error

for example


sql.Query("UPDATE player_info SET name = "..newname.." WHERE id = '"..SteamID.."'")

should be


sql.Query("UPDATE player_info SET name = '"..newname.."' WHERE id = '"..SteamID.."'")

also try using sql.LastError after each query to see if an error is being thrown