Jump to content

SQL help


Recommended Posts

So I've pretty much made this myself but for some reason it doesn't create an account. It ends up telling me "successfully created" but never adds it to the database. Code:

function accountCreate(username, password, email) 
    local preparedQuery1 = "SELECT `id` FROM `accounts` WHERE `username`='".. mysql:escape_string(username) .."' OR `email`='".. mysql:escape_string(email) .."' " 
    local Q1 = mysql:query(preparedQuery1) 
    -- check mysql conn 
    if not Q1 then 
        outputDebugString("MYSQL error when creating account.") 
        triggerClientEvent(source, "setErrorText", getRootElement(), "MySQL Error 001", 255, 0, 0) 
        return false 
    end 
    -- check if exists 
    if (mysql:num_rows(Q1) > 0) then 
        triggerClientEvent(source, "setErrorText", getRootElement(), "Username or email already exists.", 255, 0, 0) 
        mysql:free_result(Q1) 
        return false 
    end 
    -- check if player already has acc 
    local mtaSerial = getPlayerSerial(source) 
    local preparedQuery2 = "SELECT `last_serial`, `username`, `id` FROM `accounts` WHERE `last_serial`='".. toSQL(mtaSerial) .."' LIMIT 1" 
    local Q2 = mysql:query(preparedQuery2) 
    if not Q2 then 
        triggerClientEvent(source, "setErrorText", getRootElement(), "MySQL Error 002", 255, 0, 0) 
        return false 
    end 
  
    local usernameExisted = mysql:fetch_assoc(Q2) 
    if (mysql:num_rows(Q2) > 0) and usernameExisted["id"] ~= "1" then 
        triggerClientEvent(source, "setErrorText", getRootElement(), "You already have an account registered (" .. tostring(usernameExisted["username"]) .. ").", 255, 0, 0) 
        return false 
    end 
    mysql:free_result(Q2) 
     
    -- begin creating it 
    local encryptionRule = tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9))..tostring(math.random(0,9)) 
    local encryptedPW = string.lower(md5(string.lower(md5(password)) .. encryptionRule)) 
    local ipAddress = getPlayerIP(source) 
    preparedQuery3 = "INSERT INTO `accounts` SET `username`='"..toSQL(username).."', `password`='"..toSQL(encryptedPW).."', `email`='"..toSQL(email).."', `security_question` = 'nil', `security_answer` = 'nil', `last_serial`='"..mtaSerial.."', `last_ip`='"..toSQL(ipAddress).."', `salt`='"..toSQL(encryptionRule).."'" 
    local id = mysql:query_insert_free(preparedQuery3) 
    if id and tonumber(id) then 
        triggerClientEvent(source, "setErrorText", getRootElement(), "Successfully created account " .. username .. "!", 0, 255, 0) 
        return true 
    else 
        triggerClientEvent(source, "setErrorText", getRootElement(), "Error creating account.", 255, 0, 0) 
        return false 
    end 
end 
addEvent("accountCreate", true) 
addEventHandler("accountCreate", getRootElement(), accountCreate) 

All help will be appreciated.

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