Jump to content

MySQL Registration Function - DIDIR?


Recommended Posts

Posted

So, I was just wondering if someone with more experience with MySQL / MySQL-based gamemodes can tell me if I have any bugs / flaws left in this system that I should fix. In other words, Did I Do It Right?

  
addEvent("registerRequest", true) 
  
function processReqisterRequest(username, password) 
    if client and source ~= client then return end 
     
    local databaseNode = databaseConnection() 
    if not databaseNode then return end 
    local username = mysql_escape_string(databaseNode, username) 
    local origPassword = password 
    local password = mysql_escape_string(databaseNode, password) 
     
    local accountQuery = mysql_query(databaseNode, "SELECT * FROM accounts WHERE username = \'"..string.lower(username).."\'") 
    if accountQuery then 
        if mysql_num_rows(accountQuery) == 0 then 
            local serialCheck = mysql_query(databaseNode, "SELECT * FROM accounts WHERE firstSerial = \'"..getPlayerSerial(source).."\'") 
            if serialCheck then 
                if mysql_num_rows(serialCheck) < 3 then 
                    --local newAccount = mysql_query(databaseNode, "INSERT INTO accounts (username, password, registerDate, firstSerial) VALUES (\'"..string.lower(username).."\', \'"..password.."\', NOW(), \'"..getPlayerSerial(source).."\')") 
                    local newAccount = true 
                    if newAccount then 
                        triggerClientEvent(source, "registerSuccess", RESOURCE_ROOT, "You have successfully registered an account! You may now login.") 
                        mysql_free_result(newAccount) 
                    else 
                        --ERROR CODE REG1 
                        triggerClientEvent(source, "registerFailure", RESOURCE_ROOT, "Unable to perform the registration query! Please contact an admin. [ERROR CODE: REG1]") 
                    end 
                else 
                    triggerClientEvent(source, "registerFailure", RESOURCE_ROOT, "You may only register up to 3 accounts here! Contact [email protected] for account recovery instructions.") 
                    return 
                end 
                mysql_free_result(serialCheck) 
            else 
                --ERROR CODE REG2 
                triggerClientEvent(source, "registerFailure", RESOURCE_ROOT, "Unable to perform the registration query! Please contact an admin. [ERROR CODE: REG2]") 
                return 
            end 
        else 
            triggerClientEvent(source, "registerFailure", RESOURCE_ROOT, "An account with this name already exists!") 
        end 
        mysql_free_result(accountQuery) 
    else 
        --ERROR CODE REG3 
        triggerClientEvent(source, "registerFailure", RESOURCE_ROOT, "Unable to perform the registration query! Please contact an admin. [ERROR CODE: REG3]") 
        return 
    end 
end 
  

Posted (edited)

Are you sure there is databaseConnection()

No there isn't you must use "mysql_connect"

If it's a custom function you made then try:

addEvent("registerRequest", true) 
  
function processReqisterRequest(username, password) 
    local databaseNode = databaseConnection() 
    if not databaseNode then return end 
    username = mysql_escape_string(databaseNode, username) 
    local origPassword = password 
    password = mysql_escape_string(databaseNode, password) 
    
    local accountQuery = mysql_query(databaseNode, "SELECT * FROM accounts WHERE username = '"..string.lower(username).."'") 
    if accountQuery then 
        if mysql_num_rows(accountQuery) == 0 then 
            local serialCheck = mysql_query(databaseNode, "SELECT * FROM accounts WHERE firstSerial = '"..getPlayerSerial(source).."'") 
            if serialCheck then 
                if mysql_num_rows(serialCheck) < 3 then 
                    --local newAccount = mysql_query(databaseNode, "INSERT INTO accounts (username, password, registerDate, firstSerial) VALUES ('"..string.lower(username).."', '"..password.."', NOW(), '"..getPlayerSerial(source).."')") 
                    local newAccount = true 
                    if newAccount then 
                        triggerClientEvent(source, "registerSuccess", source, "You have successfully registered an account! You may now login.") 
                        mysql_free_result(newAccount) 
                    else 
                        --ERROR CODE REG1 
                        triggerClientEvent(source, "registerFailure", source, "Unable to perform the registration query! Please contact an admin. [ERROR CODE: REG1]") 
                    end 
                else 
                    triggerClientEvent(source, "registerFailure", source, "You may only register up to 3 accounts here! Contact [email protected] for account recovery instructions.") 
                    return 
                end 
                mysql_free_result(serialCheck) 
            else 
                --ERROR CODE REG2 
                triggerClientEvent(source, "registerFailure", source, "Unable to perform the registration query! Please contact an admin. [ERROR CODE: REG2]") 
                return 
            end 
        else 
            triggerClientEvent(source, "registerFailure", source, "An account with this name already exists!") 
        end 
        mysql_free_result(accountQuery) 
    else 
        --ERROR CODE REG3 
        triggerClientEvent(source, "registerFailure", source, "Unable to perform the registration query! Please contact an admin. [ERROR CODE: REG3]") 
        return 
    end 
end 

Edited by Guest
Posted

... You removed local from 'username' and 'password' ...? Then you removed if 'client and source ~= client then return end'? Ohh, and then you changed the source of the event from RESOURCE_ROOT to source? None of those changes make sense...

Posted

Why don't you understand first, there is already 2 variables called 'username' and 'password' so removing local from username and password is required, and if i'm right it might gives an error.

Yes i removed source ~= client because if i'm right source == client if you triggered the server event right.

And i also edited your triggerClientEvent replaced RESOURCE_ROOT (which i didn't find a define for it) to source which is the player.

Did you even test it?, i'm not a post count seeker, i'm just trying to help.

Posted

Ehhm, the function works - as I stated above, I'm just looking for someone who is good with the MySQL aspect of it to tell me if I've left out something important.

And "if client and source ~= client then return end" is a small form of anticheat.

Posted

You haven't done anything wrong with mysql functions.

And yes every change i did makes sense, removing local is actually critical, because you can't use local on a defined variable,

And changing RESOURCE_ROOT is important because why using resourceRoot on the base Element in triggerClientEvent, which is not even defined.

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...