Jump to content

Failed to insert row in MySQL


Recommended Posts

First time using GUI's or working with MySQL and upon using it I get the following error from server/MySQL

INFO: Registration failed. Unknown column `Phoenix` in `field list` 

Where `Phoenix` is the username

Code:

function registrationHandler(username,password) 
    local row = mysql_query(handler, "SELECT * FROM accounts WHERE username=`"..username.."`") 
    if row then 
        outputDebugString("Username already exists") 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        return 
    end 
    local result = mysql_query(handler, "INSERT INTO accounts (`accname`, `accpassword`) VALUES (`"..username.."`, `"..password.."`)") 
    if not result then 
        outputDebugString("Registration failed. "..mysql_error(handler)) 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        return 
    end 
    triggerClientEvent(client, "displayLogin", getRootElement()) 
    outputChatBox("Registration successful!", client, 0, 255, 0) 
end 
addEvent("submitRegister",true) 
addEventHandler("submitRegister",root,registrationHandler) 

Link to comment

Registration works perfect now!

Only issue is login does not show any errors in the console only thing it says is "Unable to find username"

function loginHandler(username,password) 
    local row = mysql_query(handler, "SELECT * FROM accounts WHERE accname='"..username.."'") 
    if (username ~= row) then 
        outputDebugString("Unable to find username. " .. mysql_error(handler)) 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        outputChatBox("Unable to login.") 
        return 
    end 
    if (password ~= row.password) then 
        outputDebugString("Incorrect Pasword Supplied") 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        return 
    end 
    outputChatBox("You have successfully logged in!", client, 0, 255, 0) 
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

Link to comment
    local row = mysql_query(handler, "SELECT * FROM accounts WHERE accname='"..username.."'") 
    if (username ~= row) then 

->

    local row = mysql_query( handler, "SELECT * FROM `accounts` WHERE accname = '"..username.."' " ); 
    if( mysql_num_rows( row ) == 1 ) then 
        if( row[1]['accname'] ~= username ) then 

Link to comment
  
local row = mysql_query( handler, "SELECT * FROM `accounts` WHERE accname = '"..username.."' AND password = '" .. password .. "'" ); 
    if( mysql_num_rows( row ) == 1 ) then 
        return true 
    else 
        return false 
    end 
  

Link to comment

Um.. I'm not sure what you want me to do with that snippet.

EDIT: Nevermind, I got it but now it shows "Incorrect Password Supplied" :?

Code:

function loginHandler(username,password,thePlayer) 
    local row = mysql_query( handler, "SELECT * FROM `accounts` WHERE accname = '"..username.."' " ); 
    if( mysql_num_rows( row ) == 1 ) then 
        if (row['password'] == password) then 
            outputChatBox("You have successfully logged in!", client, 0, 255, 0) 
            return 
        end 
    else 
        outputChatBox("Unable to find username", client, 255, 0, 0) 
    end 
    outputDebugString("Incorrect Pasword Supplied") 
    triggerClientEvent(client, "displayLogin", getRootElement()) 
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

Link to comment
function loginHandler(username,password) 
    local row = mysql_query(handler, "SELECT * FROM accounts WHERE accname='" .. username .. "' AND password='" .. password .. "'") 
    if row then 
        outputChatBox("You have successfully logged in!", client, 0, 255, 0) 
        mysql_free_result(row) 
        return true 
    else 
        outputChatBox("Your username and/or password is incorrect!", client, 0, 255, 0) 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        return false 
    end 
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

Edited by Guest
Link to comment

*facepalm* this is the second time this has happened to me, it's accpassword. Gonna fix it and I'll let you know if it works.

EDIT: Any password logs you in.

function loginHandler(username,password) 
    local row = mysql_query(handler, "SELECT * FROM accounts WHERE accname='" .. username .. "' AND accpassword='" .. password .. "'") 
    if row then 
        outputChatBox("You have successfully logged in!", client, 0, 255, 0) 
        mysql_free_result(row) 
        return true 
    else 
        outputChatBox("Your username and/or password is incorrect!", client, 255, 0, 0) 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        return false 
    end 
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 
  

EDIT 2:

Switching back to this

function loginHandler(username,password) 
    local row = mysql_query(handler, "SELECT * FROM accounts WHERE accname='" .. username .. "' AND accpassword='" .. password .. "'") 
    if( mysql_num_rows( row ) == 1 ) then 
        outputChatBox("You have successfully logged in!", client, 0, 255, 0) 
        return true 
    else 
        outputChatBox("Your username and/or password is incorrect!", client, 255, 0, 0) 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        return false 
    end 
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

Works perfectly. Thanks so much. :)

Link to comment

Yes, it works. :)

Now using your code:

function loginHandler(username,password) 
    local row = mysql_query(handler, "SELECT * FROM accounts WHERE accname='" .. username .. "' AND accpassword='" .. password .. "'") 
    if( mysql_num_rows( row ) == 1 ) then 
        outputChatBox("You have successfully logged in!", client, 0, 255, 0) 
        return true 
    else 
        outputChatBox("Your username and/or password is incorrect!", client, 255, 0, 0) 
        triggerClientEvent(client, "displayLogin", getRootElement()) 
        return false 
    end 
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

Will I still be able to pull data using row.x or row[x]? Or how would I go about doing this?

Link to comment
setElementData(client, "logged", 1) 
        setElementData(client, "account:admin", row["admin"]) 
        if (getElementData(client,"account:admin") > 0) then 
            AStatus = getElementData(client,"account:admin") 
            if (AStatus == 1) then 
                outputChatBox("You are playing as a PR Moderator.", client, 0, 0, 255) 
            elseif (AStatus == 2) then 
                outputChatBox("You are playing as a PR Administrator.", client, 0, 0, 255) 
            elseif (AStatus == 3) then 
                outputChatBox("Welcome back, Phoenix.", client, 0, 0, 255) 
            end 
        end 

ERROR: pf\s_main.lua:17: attempt to compare number with nil 

Link to comment
setElementData(client, "logged", 1) 
setElementData(client, "account:admin", tonumber(row["admin"]) or 3) 
local AStatus = tonumber(row["admin"]) or 3 
if (AStatus == 1) then 
    outputChatBox("You are playing as a PR Moderator.", client, 0, 0, 255) 
elseif (AStatus == 2) then 
    outputChatBox("You are playing as a PR Administrator.", client, 0, 0, 255) 
elseif (AStatus == 3) then 
    outputChatBox("Welcome back, Phoenix.", client, 0, 0, 255) 
end 

Did you forget about tonumber? ;/

Edited by Guest
Link to comment

I tried yours, it works without a doubt due to the fact that if account:admin was not set to anything above 0 (at least that we know of) it sets it to 3, so I changed it and switched it to 2 instead and whenever I join it says "PF Administrator" the issue it seems to be having is pulling the data from the row, as before we we're having this issue.

Anyways, not getting any errors at all.

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