1. Post #1
    Gold Member
    antid2's Avatar
    August 2008
    640 Posts
    Okay, everything works good, except it's not updating my database and i don't understand why, i use almost the same code for money, except i can't set my money, nor do i intend to lol...But here's the code

    names.lua
    function Regname(ply)
    local name = ply:GetNWString("name")
    
    	if not tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")) then
    		sql.Query("INSERT INTO player_name VALUES("..sql.SQLStr(ply:SteamID())..",".. name ..");")
    	else
    	sql.Begin()
    	ply:SetNWString("name",tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
    	sql.Commit()
    	end
    end
    hook.Add("PlayerInitialSpawn", "Regname", Regname)
    

    commands.lua
    //commands
    function SetName(ply,cmd,args)
    local PlayerName = tostring(args[1])
    ply:SetNWString("name", PlayerName)
    		sql.Begin()
    		sql.Query("UPDATE player_name SET name = "..PlayerName.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";") 
    		sql.Commit()
    end
    concommand.Add("ChangeName", SetName)
    
    and client side in cl_startmenu.lua
    Apply.DoClick = function( btn )
    		if FirstName:GetValue() == "" then
    			ply:ChatPrint("You did not enter a first name")
    		else
    		RunConsoleCommand("ChangeName", FirstName:GetValue())
    		Frame:Remove()
    		end
    	end
    

    It sets the name, etc..but in commands.lua it's not updating the database..
    Reply With Quote Edit / Delete Reply United States Show Events Dumb Dumb x 1 (list)

  2. Post #2

    February 2010
    10 Posts
    hello antid2

    did u remember to do "require(mysql)"
    ?

    jess
    Reply With Quote Edit / Delete Reply Germany Show Events Dumb Dumb x 1 (list)

  3. Post #3
    Gold Member
    Entoros's Avatar
    October 2008
    2,318 Posts
    Go away troll.

    When you create the table, do you set the key to steam?

  4. Post #4
    Gold Member
    antid2's Avatar
    August 2008
    640 Posts
    oh sorry..i must have cut some SQL off...heres the full names.lua
    
    sql.Query("CREATE TABLE IF NOT EXISTS player_name('steam' TEXT NOT NULL, 'name' INTEGER NOT NULL,PRIMARY KEY('steam'));")
    function UpdateName(ply)
    	local name = ply:GetNWString("name") 
    	sql.Begin()
    	sql.Query("UPDATE player_name SET name = "..name.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";") 
    	sql.Commit()
    end
    hook.Add("PlayerDisconnected", "UpdateName", UpdateName)
    function Regname(ply)
    local name = ply:GetNWString("name")
    
    	if not tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")) then
    		sql.Query("INSERT INTO player_name VALUES("..sql.SQLStr(ply:SteamID())..",".. name ..");")
    	else
    	sql.Begin()
    	ply:SetNWString("name",tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
    	sql.Commit()
    	end
    end
    hook.Add("PlayerInitialSpawn", "Regname", Regname)
    
    

  5. Post #5
    quincy18's Avatar
    September 2007
    1,005 Posts
    Why are you using tonumber when your working with names ?

    never used the external database module so excuse me if thats why your using it.

  6. Post #6
    Pass along
    commander204's Avatar
    May 2008
    4,065 Posts
    this isnt.

  7. Post #7
    I fucking love eggs!!!
    Averice's Avatar
    July 2007
    1,991 Posts
    Should name be an INTEGER NOT NULL?

    Just guessing as you said you use the exact same for your money system, maybe you didn't change it when you copy and pasted it.

    And seeing as "steam" is the primary key I'm going to guess name should be TEXT NOT NULL;

    sql.Query("CREATE TABLE IF NOT EXISTS player_name('steam' TEXT NOT NULL, 'name' TEXT NOT NULL,PRIMARY KEY('steam'));")  
    

  8. Post #8
    Gold Member
    antid2's Avatar
    August 2008
    640 Posts
    good catch averice, didn't notice that.


    Edit:
    New file:
    
    sql.Query("CREATE TABLE IF NOT EXISTS player_name('steam' TEXT NOT NULL, 'name' TEXT NOT NULL,PRIMARY KEY('steam'));")
    function UpdateName(ply)
    	local name = ply:GetNWString("name") 
    	sql.Begin()
    	sql.Query("UPDATE player_name SET name = "..name.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";") 
    	sql.Commit()
    end
    hook.Add("PlayerDisconnected", "UpdateName", UpdateName)
    function Regname(ply)
    local name = ply:GetNWString("name")
    
    	if not tonumber(sql.QueryValue("SELECT name FROM player_name WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")) then
    		sql.Query("INSERT INTO player_name VALUES("..sql.SQLStr(ply:SteamID())..",".. name ..");")
    	else
    	sql.Begin()
    	ply:SetNWString("name",tostring(sql.QueryValue("SELECT name FROM player_name WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
    	sql.Commit()
    	end
    end
    hook.Add("PlayerInitialSpawn", "Regname", Regname)
    
    

    Still no go boss.
    Reply With Quote Edit / Delete Reply United States Show Events Dumb Dumb x 1 (list)

  9. Post #9
    cheiftiger's Avatar
    March 2009
    231 Posts
    You don't have to use sql.Begin() and sql.Commit() if you're just doing 1-2 queries. That's used for alot of queries at once (maybe 5 or more). And stop using tonumber() on strings. That will just return nil, which is why it's not saving probably.

    I don't know if you've seen it, but there's a tutorial on GMod SQL here:

    http://wiki.garrysmod.com/?title=LUA:SQLite_Tutorial

    I recently made an entity using SQL, and I used varchar(255) for strings. I don't know if that makes a difference, but mine works. I also didn't do that PRIMARYKEY thing that you did.

    That's the best help I can give without coding the thing for you, so I wish you good luck.

  10. Post #10
    Gold Member
    antid2's Avatar
    August 2008
    640 Posts
    that would be amazing if the code worked :P, i modified it just to test, didn't work...so i figured maybe i messed up...i copied the code straight from the wiki and it says something went wrong with creating a players info, etc. and savestat doesn't work :(