Jump to content

Problem with ID


Hamatora

Recommended Posts

Hello guys,

I need help with this simple server script. Everything is working fine, the problem is that when I delete ID from the table. Take a look:

function onResourceStart()
    executeSQLQuery("CREATE TABLE IF NOT EXISTS testing (id INT, text TEXT, name TEXT)")
end
addEventHandler("onResourceStart", resourceRoot, onResourceStart)

function onChat(msg, msgType)
    if msgType == 0 then
        theTable = executeSQLQuery("SELECT * FROM testing")
        executeSQLQuery("INSERT INTO testing (id, text, name) VALUES (?,?,?)", #theTable + 1, msg, getPlayerName(source))
    end
end
addEventHandler("onPlayerChat", root, onChat)

function showTable()
    theTable = executeSQLQuery("SELECT * FROM testing")
    for i,v in ipairs(theTable) do
        outputChatBox("ID: "..tostring(v.id).." NAME: "..tostring(v.name).." MSG: "..tostring(v.text))
    end
end
addCommandHandler("showtable", showTable)

function deleteFromTable(source, cmd, id)
    executeSQLQuery("DELETE FROM testing WHERE id=?", id)
end
addCommandHandler("delete", deleteFromTable)

 Suppose the table is:

ID: 1 NAME: Hamatora MSG: test
ID: 2 NAME: Hamatora MSG: test
ID: 3 NAME: Hamatora MSG: test

-> Use /delete 2

Means ID 2 will be deleted.

-> Now chat

-> Now use /showtable

You will see that ID 3 is repeated two times, thats the problem

Link to comment
function onResourceStart()
	executeSQLQuery("CREATE TABLE IF NOT EXISTS testing (id INT, text TEXT, name TEXT)")
end
addEventHandler("onResourceStart", resourceRoot, onResourceStart)

function onChat(msg, msgType)
	if msgType == 0 then
		theTable = executeSQLQuery("SELECT * FROM testing")
		nextID = theTable[#theTable]["id"] + 1
		executeSQLQuery("INSERT INTO testing (id, text, name) VALUES (?,?,?)", nextID, msg, getPlayerName(source))
	end
end
addEventHandler("onPlayerChat", root, onChat)

function showTable()
	theTable = executeSQLQuery("SELECT * FROM testing")
	for i,v in ipairs(theTable) do
		outputChatBox("ID: "..tostring(v.id).." NAME: "..tostring(v.name).." MSG: "..tostring(v.text))
	end
end
addCommandHandler("showtable", showTable)

function deleteFromTable(source, cmd, id)
	executeSQLQuery("DELETE FROM testing WHERE id=?", id)
end
addCommandHandler("delete", deleteFromTable)

Try this one.

Edited by NeXuS™
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...