SQL connection and derma menu

Hey, so I’m making a derma menu that you can use to report bugs on the server and a database will record your bug and send it to the DB. I’m unsure as to why it isn’t working currently. There are no errors at all.

It’s installed in addons/bugReporter/lua/autorun

client-
[lua]
function BugReportDerma()
local bugReporterDerma = vgui.Create(“DFrame”)
bugReporterDerma:SetSize(500,110)
bugReporterDerma:SetTitle(“Suggestion/Bug Reporter”)
bugReporterDerma:Center()
bugReporterDerma:SetVisible(true)
bugReporterDerma:SetDraggable(true)
bugReporterDerma:ShowCloseButton(true)
bugReporterDerma:SetBackgroundBlur(true)
bugReporterDerma:MakePopup()

net.Receive("BugDBConnect")

local bugInput = vgui.Create("DTextEntry", bugReporterDerma)
bugInput:SetPos( 5, 25 )
bugInput:SetSize( 490, 50 )
bugInput:SetText( "Provide a description of the bug you found:" )
bugInput.OnEnter = function( self )
	net.Receive( "bug_report_value", function() 
		net.WriteString( self:GetValue() )
		net.SendToServer()
	end)
end

local CloseButton = vgui.Create( "DButton", bugReporterDerma)
CloseButton:SetText( "Close" )
CloseButton:SetTextColor( Color( 0, 0, 0 ) )
CloseButton:SetPos( 5, 80 )
CloseButton:SetSize( 490, 25 )
CloseButton.DoClick = function()
    bugReporterDerma:Close()
end

end
concommand.Add(“bug_reporter”, BugReportDerma)
[/lua]

server-
[lua]
require( “tmysql4” )

local DATABASE_HOST = “-snip-”
local DATABASE_PORT = 3306
local DATABASE_NAME = “-snip-”
local DATABASE_USERNAME = “-snip-”
local DATABASE_PASSWORD = “-snip-”

util.AddNetworkString( “bug_report_value” )
util.AddNetworkString( “BugDBConnect” )
net.Start(“bug_report_value”)

net.Start(“BugDBConnect”, function()
BugDB = tmysql.initialize(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT)
BugDB:Connect()
end)

function BugReportReceived(ply)
bug_report_value = net.ReadString()
ply:PrintMessage(HUD_PRINTCONSOLE, "Recieved Value: " … bug_report_value)
SaveBugToDatabase(ply, bug_report_value)
end

net.Receive(“bug_report_value”, function BugReportReceived(ply))

BugDB:Query( “UPDATE bug_report SET BugReport = " … BugReport … " WHERE SteamID = '” … ply:SteamID() … “’”, BugReportReceived )[/lua]

Read the wiki.

Fixed that but the problem is I need to be able to call ply within the DB query and I cannot because you can’t call DB:Query within a function I believe.

Current code.

client-
[lua]
function BugReportDerma()
local bugReporterDerma = vgui.Create(“DFrame”)
bugReporterDerma:SetSize(500,110)
bugReporterDerma:SetTitle(“Suggestion/Bug Reporter”)
bugReporterDerma:Center()
bugReporterDerma:SetVisible(true)
bugReporterDerma:SetDraggable(true)
bugReporterDerma:ShowCloseButton(true)
bugReporterDerma:SetBackgroundBlur(true)
bugReporterDerma:MakePopup()

local bugInput = vgui.Create("DTextEntry", bugReporterDerma)
bugInput:SetPos( 5, 25 )
bugInput:SetSize( 490, 50 )
bugInput:SetText( "Provide a description of the bug you found:" )
bugInput.OnEnter = function( self )
end

local CloseButton = vgui.Create( "DButton", bugReporterDerma)
CloseButton:SetText( "Close" )
CloseButton:SetTextColor( Color( 0, 0, 0 ) )
CloseButton:SetPos( 5, 80 )
CloseButton:SetSize( 490, 25 )
CloseButton.DoClick = function()
    bugReporterDerma:Close()
end

end
concommand.Add(“bug_reporter”, BugReportDerma)

net.Receive(“BugDBConnect”, BugReportDerma)

net.Receive( “bug_report_value”, function()
net.WriteString( BugReportDerma.bugInput:GetValue(self) )
net.SendToServer()
end)[/lua]

server-
[lua]
require( “tmysql4” )

-Local login shits-

local BugDB = tmysql.Connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORTS)

util.AddNetworkString( “bug_report_value” )
util.AddNetworkString( “BugDBConnect” )
net.Start(“bug_report_value”)

net.Start(“BugDBConnect”, function()
–BugDB = tmysql.initialize(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT)
BugDB:Connect()
end)

–[[
function BugReportReceived(ply)
bug_report_value = net.ReadString()
ply:PrintMessage(HUD_PRINTCONSOLE, "Recieved Value: " … bug_report_value)
SaveBugToDatabase(ply, bug_report_value)
end
]]

net.Receive(“bug_report_value”, function()
bug_report_value = net.ReadString()
ply:PrintMessage(HUD_PRINTCONSOLE, "Recieved Value: " … bug_report_value)
SaveBugToDatabase(ply, bug_report_value)
end)

function TestFunc(ply)
bug_report_value = net.ReadString()
ply:PrintMessage(HUD_PRINTCONSOLE, "Recieved Value: " … bug_report_value)
SaveBugToDatabase(ply, bug_report_value)
end

local function GetSteamID(ply)
BugDB:Query( “UPDATE bug_report SET BugReport = " … BugReport … " WHERE SteamID = '” … ply:SteamID() … “’”, TestFunc)
end
[/lua]

Uh lol, you’ve exposed your MySQL credentials in that post. You’d better change the password now.

Spooky,

There’s also this. :v:

EDIT:
Don’t store passwords in MD5. Please.
[t]http://i.imgur.com/UXKv7eY.png

Rip i forgot to do that


function BugReportDerma()
	local bugReporterDerma = vgui.Create("DFrame")
	bugReporterDerma:SetSize(500,110)
    bugReporterDerma:SetTitle("Suggestion/Bug Reporter")
    bugReporterDerma:Center()
   	bugReporterDerma:SetVisible(true)
    bugReporterDerma:SetDraggable(true)
    bugReporterDerma:ShowCloseButton(true)
    bugReporterDerma:SetBackgroundBlur(true)
    bugReporterDerma:MakePopup()

    local bugInput = vgui.Create("DTextEntry", bugReporterDerma)
    bugInput:SetPos( 5, 25 )
    bugInput:SetSize( 490, 50 )
    bugInput:SetText( "Provide a description of the bug you found:" )
    bugInput.OnEnter = function( self )
	end

    local CloseButton = vgui.Create( "DButton", bugReporterDerma)
    CloseButton:SetText( "Close" )
    CloseButton:SetTextColor( Color( 0, 0, 0 ) )
    CloseButton:SetPos( 5, 80 )
    CloseButton:SetSize( 490, 25 )
    CloseButton.DoClick = function()
        bugReporterDerma:Close()
    end
end
concommand.Add("bug_reporter", BugReportDerma)

net.Receive("BugDBConnect", BugReportDerma)

net.Receive( "bug_report_value", function() 
    net.WriteString( BugReportDerma.bugInput:GetValue(self) )
    net.SendToServer()
end)

first of all


net.Receive( "bug_report_value", function() 
    net.WriteString( BugReportDerma.bugInput:GetValue(self) )
    net.SendToServer()
end)

this is using the receive function and trying to use a fucking function as a table
instead you would do this


net.Start("bug_report_value")
net.WriteString( bugInput:GetValue(self) )
net.SendToServer()

but now it’s still sending a nil value and not even being called when we want it to
so now we need to put it in that cool button’s doclick function


    local CloseButton = vgui.Create( "DButton", bugReporterDerma)
    CloseButton:SetText( "Submit and Exit" )
    CloseButton:SetTextColor( Color( 0, 0, 0 ) )
    CloseButton:SetPos( 5, 80 )
    CloseButton:SetSize( 490, 25 )
    CloseButton.DoClick = function()
    	net.Start("bug_report_value")
		net.WriteString(BugReportDerma.bugInput:GetValue(self))
		net.SendToServer()
        bugReporterDerma:Close()
    end

amazing i know
fixed code:


function BugReportDerma()
	local bugReporterDerma = vgui.Create("DFrame")
	bugReporterDerma:SetSize(500,110)
    bugReporterDerma:SetTitle("Suggestion/Bug Reporter")
    bugReporterDerma:Center()
   	bugReporterDerma:SetVisible(true)
    bugReporterDerma:SetDraggable(true)
    bugReporterDerma:ShowCloseButton(true)
    bugReporterDerma:SetBackgroundBlur(true)
    bugReporterDerma:MakePopup()

    local bugInput = vgui.Create("DTextEntry", bugReporterDerma)
    bugInput:SetPos( 5, 25 )
    bugInput:SetSize( 490, 50 )
    bugInput:SetText( "Provide a description of the bug you found:" )
    bugInput.OnEnter = function( self )
	end

    local CloseButton = vgui.Create( "DButton", bugReporterDerma)
    CloseButton:SetText( "Submit and Exit" )
    CloseButton:SetTextColor( Color( 0, 0, 0 ) )
    CloseButton:SetPos( 5, 80 )
    CloseButton:SetSize( 490, 25 )
    CloseButton.DoClick = function()
    	net.Start("bug_report_value")
		net.WriteString(BugReportDerma.bugInput:GetValue(self))
		net.SendToServer()
        bugReporterDerma:Close()
    end
end

concommand.Add("bug_reporter", BugReportDerma)
net.Receive("BugDBConnect", BugReportDerma)

maybe learn some more lua before trying to make something like this

– edit –

oh boy looks like i missed even more shit
on the server:



net.Start("BugDBConnect", function()
	--BugDB = tmysql.initialize(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT)
	BugDB:Connect()
end)

that’s not how you use net.Start
and you’re not actually sending it, you just have an open net message

i don’t know what that shit even is trying to do (init mysql after it can?)
if it’s just connecting the db then just do this


hook.Add("InitPostEntity", "ConnectBugDB", function()
BugDB:Connect()
hook.Remove("InitPostEntity", "ConnectBugDB")
end)

Alright, thank you for clearing some stuff up. I haven’t done anything serious with lua in quite a long time and I’ve shifted my focus from it so I made a couple mistakes. Thank you for showing me what was incorrect.

However, it still does not insert any data to the database.

That was a simple user reg system I was working with my friend on. Nothing serious was getting done yet.

post the SaveBugToDatabase function

– edit –

are you sure the code is even being ran – the server part should be erroring
also remove thr net.Start(“bug_report_value”) at the start of the script

[lua]
require(“tmysql4”)

local BugDB = tmysql.Connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORTS)

util.AddNetworkString( “bug_report_value” )
util.AddNetworkString( “BugDBConnect” )

function TestFunc(ply)
bug_report_value = net.ReadString()
ply:PrintMessage(HUD_PRINTCONSOLE, "Recieved Value: " … bug_report_value)
SaveBugToDatabase(ply, bug_report_value)
end

local bug_report_value = net.ReadString()
local function GetSteamID(ply)
BugDB:Query( “UPDATE bug_report SET BugReport = " … bug_report_value … " WHERE SteamID = '” … ply:SteamID() … “’”, TestFunc )
end[/lua]
Is everything in the serverside file right now. Calling the second connect made it error out because it was already connecting in the variable BugDB.



net.Start 'bug_report_value'
    net.WriteString 'escape me plz; /*'
net.SendToServer()


also why do you have a network string to connect the db? just let it connect on server start.

That was something I forgot to remove. We already handled the strings being sent on the clientside also.

Edit:
I realized that it probably isn’t sending because I had it writing in a function, and the function was never called, so first i called the function, and ply is a nil value even when i cast ply is in the function, and it, obviously, isn’t recognized outside of a function.


require("tmysql4")

local BugDB = tmysql.Connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORTS)

util.AddNetworkString( "bug_report_value" )
util.AddNetworkString( "BugDBConnect" ) // ????

function TestFunc(ply) // what the fuck even is this, never being called either
	bug_report_value = net.ReadString()
	ply:PrintMessage(HUD_PRINTCONSOLE, "Recieved Value: " .. bug_report_value)
	SaveBugToDatabase(ply, bug_report_value) // not a function
end

local bug_report_value = net.ReadString() // outside of a net.Receive callback
local function GetSteamID(ply) // never being called, and arguments for net.Receive callback == len, ply
	BugDB:Query( "UPDATE bug_report SET BugReport = " .. bug_report_value .. " WHERE SteamID = '" .. ply:SteamID() .. "'", TestFunc )
end

fixed code:


util.AddNetworkString( "bug_report_value" )
require("tmysql4")

local BugDB = tmysql.Connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORTS)
hook.Add("InitPostEntity", "ConnectBugDB", function()
    BugDB:Connect()
end)

local function GetSteamID(len, ply)
    local bug_report_value = net.ReadString()
    BugDB:Query( "UPDATE bug_report SET BugReport = " .. bug_report_value .. " WHERE SteamID = '" .. ply:SteamID() .. "'", TestFunc )
end

net.Receive("bug_report_value", GetSteamID)

With your fix I added a PrintMessage statement to make sure it was reaching that point in the code, and it is, so thank you. However it still does not write to the database so I must have fucked up the SQL code. I’ll look into it. Thanks for the help.

[lua]
util.AddNetworkString( “bug_report_value” )
require(“tmysql4”)

local DATABASE_HOST = “”
local DATABASE_PORT = 3306
local DATABASE_NAME = “”
local DATABASE_USERNAME = “”
local DATABASE_PASSWORD = “”

local BugDB = tmysql.Connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORTS)
hook.Add(“InitPostEntity”, “ConnectBugDB”, function()
BugDB:Connect()
end)

local function GetSteamID(len, ply)
local bug_report_value = net.ReadString()
ply:PrintMessage(HUD_PRINTTALK , “Success!”)
BugDB:Query( “UPDATE bug_report SET BugReport = " … bug_report_value … " WHERE SteamID = '” … ply:SteamID() … “’”)
end

net.Receive(“bug_report_value”, GetSteamID)[/lua]