Jump to content

[MySQL] INSERT not return nothing


Recommended Posts

I try to do a registration system, but the INSERT clause not return nothing.

This is my code

local MYSQL = dbConnect("mysql", "dbname=MtaBG;host=127.0.0.1", "root", "") 
  
  
function ComprobarLogin(PlayerName, PlayerPass) -- Comprobar si esta registrado o no 
    local MitadString = math.floor(string.len(PlayerName)/2) 
    -- outputChatBox("User Name: " .. string.sub(PlayerName, 0, MitadString) .. "; Password: " .. string.sub(PlayerName, MitadString+1), client) 
end 
addEvent("CheckLoginEvent", true) -- Agregar el evento que se llama desde client_login.lua 
addEventHandler("CheckLoginEvent", resourceRoot, ComprobarLogin) -- Llamar a la función cuando se llama el evento 
  
function ComprobarRegistro(PlayerName, PlayerPass) 
    local MitadString = math.floor(string.len(PlayerName)/2) 
    local contra = hash("sha512", string.sub(PlayerName, 0, MitadString) .. PlayerPass .. string.sub(PlayerName, MitadString+1)) 
    dbQuery(ComprobarCuentaEnDB, MYSQL, "INSERT INTO `userdata`(`Cuenta`, `Password`) VALUES (?, ?)", PlayerName, contra) 
end 
  
function ComprobarCuentaEnDB(hand)  
    outputChatBox("Start ComprobarCuentaEnDB") 
  
    local result, num_affected_rows, last_insert_id = dbPoll (hand, 0) 
  
    if result == nil then 
        outputConsole( "dbPoll result not ready yet" ) 
    elseif result == false then 
        local error_code,error_msg = num_affected_rows,last_insert_id 
        outputConsole( "dbPoll failed. Error code: " .. tostring(error_code) .. "  Error message: " .. tostring(error_msg) ) 
    else 
        outputConsole( "dbPoll succeeded. Number of affected rows: " .. tostring(num_affected_rows) .. "  Last insert id: " .. tostring(last_insert_id) ) 
    end     
    outputChatBox("End ComprobarCuentaEnDB") 
end  
  
addEvent("CheckRegisterEvent", true) 
addEventHandler("CheckRegisterEvent", resourceRoot, ComprobarRegistro) 

The INSERT works, insert a row in mysql.

Link to comment
    dbQuery(ComprobarCuentaEnDB, MYSQL, "INSERT INTO `userdata`(`Cuenta`, `Password`) VALUES (?, ?)", PlayerName, contra) 

replace to

    local success = dbExec(ComprobarCuentaEnDB, MYSQL, "INSERT INTO `userdata`(`Cuenta`, `Password`) VALUES (?, ?)", PlayerName, contra) 
if success then 
-- correctly 
else 
-- error 
end 

Link to comment

Thanks for reply.

I try the code and return true when consult executed correctly. But in console appear one warning:

WARNING: generalgame\server_login.lua:15: dbExec failed; (1062) Duplicata du champ 'Nicolas' pour la clef 'Cuenta'

How do i check if the consult is execute correctly?

Link to comment
  • 3 weeks later...
    dbQuery(ComprobarCuentaEnDB, MYSQL, "INSERT INTO `userdata`(`Cuenta`, `Password`) VALUES (?, ?)", PlayerName, contra) 

replace to

    local success = dbExec(ComprobarCuentaEnDB, MYSQL, "INSERT INTO `userdata`(`Cuenta`, `Password`) VALUES (?, ?)", PlayerName, contra) 
if success then 
-- correctly 
else 
-- error 
end 

That won't actually work. If you do something like dbExec(db, "INSERT") it will return true, as it only returns false if the connection is incorrect (first argument).[1]

[1] = dbExec

Thanks for reply.

I try the code and return true when consult executed correctly. But in console appear one warning:

WARNING: generalgame\server_login.lua:15: dbExec failed; (1062) Duplicata du champ 'Nicolas' pour la clef 'Cuenta'

How do i check if the consult is execute correctly?

As for your problem, I'm not sure. You might just have to query for the data straight afterwards. Just reuse the variables you had before to query.

Link to comment

1062 is an sql error which states;

Duplicate entry '#' for key '#'

Make sure your 'id' is autoincrement, make sure your username isn't an unique value in your database. Better thing to do is query your database first to see if there is someone using that name already. If not, insert it.

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