Jump to content

MTA Scripting, Test MySQL Script!


Recommended Posts

CLIENT SIDE

function saveBusiness() 
if source == btnCreateBus then 
local getBusName = guiGetText ( edName ) 
if getBusName then 
triggerServerEvent("onBusinessCreate", getLocalPlayer(), getBusName) 
     end 
  end 
end 
addEventHandler("onClientGUIClick", resourceRoot, saveBusiness) 

SERVER SIDE

mysql = exports.mysql 
  
function createBusiness(getBusName) 
        mysql:query_free("INSERT INTO business (name)", getBusName) 
        outputChatBox("Congratulations, you have created your business", source, 255, 194, 14) 
    end 
addEvent("onBusinessCreate", true) 
addEventHandler("onBusinessCreate", getRootElement(), createBusiness) 

It outputs no errors?

Any help would be appreciated, as I can then continue learning MySQL!

Link to comment
mysql = exports.mysql 
      
    function createBusiness(name) 
            local insert = mysql:query_free("INSERT INTO business SET name=".. mysql:escape_string(name) .."") 
        if (insert) then 
            outputChatBox("Congratulations, you have created your business", source, 255, 194, 14) 
        else 
            outputDebugString("Mysql Error: Unable to create business!") 
            mysql:free_result(insert) 
        end 
    end 
    addEvent("onBusinessCreate", true) 
    addEventHandler("onBusinessCreate", getRootElement(), createBusiness) 

Link to comment

Well first check what's the name of the collumn of your MySQL database where the name of the business goes and change "name" to the name of the collumn. Also, getBusName is just a name for the function arguments, so it doesn't matter how you name it in the function and trigger if they are in the same order.

EG: If in server side you have "function testFunction (thePlayer, argument1, argument2)" and in client you trigger something like this "triggerServerEvent ( "testFunction", thePlayer, arg1, arg2 )" then what you have for arg1 and arg2 will output in argument1 and argument2... I'm not really good at explaining :P

Link to comment
mysql = dbConnect( "mysql", "dbname=thedb;host=localhost", "root", "", "share=1" ) 
      
    function createBusiness(getBusName) 
            local insert = dbExec( mysql, "INSERT INTO business VALUES (name)", getBusName ) 
        if (insert) then 
            outputChatBox("Congratulations, you have created your business", source, 255, 194, 14) 
        else 
            outputDebugString("MySQL Error: Unable to create business!") 
        end 
    end 
    addEvent("onBusinessCreate", true) 
    addEventHandler("onBusinessCreate", getRootElement(), createBusiness) 

What if I was to do this? It still outputs no error :(

Link to comment

Try this:

local mysql = exports.mysql 
  
function createBusiness(getBusName) 
    mysql:query_free("INSERT INTO business ( 'name' ) VALUES ( ? )", getBusName) 
    outputChatBox("Congratulations, you have created your business", source, 255, 194, 14) 
end 
addEvent("onBusinessCreate", true) 
addEventHandler("onBusinessCreate", getRootElement(), createBusiness) 

If that doesn't work, try using this:

local mysql = exports.mysql 
  
function createBusiness(getBusName) 
    mysql:query_free("INSERT INTO business ( 'name' ) VALUES ( '".. getBusName .." )" ) 
    outputChatBox("Congratulations, you have created your business", source, 255, 194, 14) 
end 
addEvent("onBusinessCreate", true) 
addEventHandler("onBusinessCreate", getRootElement(), createBusiness) 

However in the second one, unless the query_free function filters the query, sql injection will be possible. If the first one doesn't work, please post the query_free function.

Link to comment
mysql = dbConnect( "mysql", "dbname=thedb;host=localhost", "root", "", "share=1" ) 
      
    function createBusiness(getBusName) 
            local insert = dbExec( mysql, "INSERT INTO business ( name ) VALUES ( ? )", getBusName ) 
        if (insert) then 
            outputChatBox("Congratulations, you have created your business", source, 255, 194, 14) 
        else 
            outputDebugString("MySQL Error: Unable to create business!") 
        end 
    end 
    addEvent("onBusinessCreate", true) 
    addEventHandler("onBusinessCreate", getRootElement(), createBusiness) 

be sure that the sql is connected

Link to comment

How you can be sure that the event triggred if there's no errors or anything outputted

put outputDebugString at the first line of the "createBusiness" function

to be sure that it's beeing triggred

mysql = dbConnect( "mysql", "dbname=thedb;host=localhost", "root", "", "share=1" ) 
      
    function createBusiness(getBusName) 
    outputDebugString("onBusinessCreate Has been triggred !") 
            local insert = dbExec( mysql, "INSERT INTO business ( name ) VALUES ( ? )", getBusName ) 
        if (insert) then 
            outputChatBox("Congratulations, you have created your business", source, 255, 194, 14) 
        else 
            outputDebugString("MySQL Error: Unable to create business!") 
        end 
    end 
    addEvent("onBusinessCreate", true) 
    addEventHandler("onBusinessCreate", getRootElement(), createBusiness) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...