Jump to content

INSERT INTO. mysql


Jmaniacs

Recommended Posts

Vale, me he complicado en este pequeño script para practicar, pero nose por que no funciona, esta raro. porfavor agradeceria su ayuda.

 

Client-side:

Spoiler

local screenW, screenH = guiGetScreenSize()
function xd()
window = guiCreateWindow((screenW - 489) / 2, (screenH - 236) / 2, 489, 236, "Vehicle Spawner", false)
guiWindowSetSizable(window, false)
spawnbutton = guiCreateButton(0.30, 0.73, 0.42, 0.19, "Spawn!", true, window)
guiSetFont(spawnbutton, "default-bold-small")
guiSetProperty(spawnbutton, "NormalTextColour", "FFAAAAAA")
vehId = guiCreateEdit(0.10, 0.19, 0.22, 0.10, "411", true, window)
vehName = guiCreateEdit(0.62, 0.37, 0.22, 0.10, "Skyline", true, window)
vehYear = guiCreateEdit(0.10, 0.37, 0.22, 0.10, "2018", true, window)
vehBrand = guiCreateEdit(0.62, 0.19, 0.22, 0.10, "Nissan", true, window)
vehPrice = guiCreateEdit(0.34, 0.55, 0.35, 0.10, "15000", true, window)
text = guiCreateLabel(0.10, 0.13, 0.13, 0.06, "GTA ID", true, window)
text2 = guiCreateLabel(0.10, 0.31, 0.15, 0.06, "Vehicle Year", true, window)
text3 = guiCreateLabel(0.62, 0.13, 0.15, 0.06, "Vehicle Brand", true, window)
text4 = guiCreateLabel(0.62, 0.31, 0.15, 0.06, "Vehicle Name", true, window)
text5 = guiCreateLabel(0.34, 0.49, 0.35, 0.06, "Vehicle Price", true, window)
guiLabelSetHorizontalAlign(text5, "center", false) 
addEventHandler("onClientGUIClick", spawnbutton, sendSpawn)  
guiSetVisible(window, true)
end
addEventHandler("onClientResourceStart", root, xd)

function sendSpawn()
	id = guiGetText(vehId)
	name = guiGetText(vehName)
	year = guiGetText(vehYear)
	brand = guiGetText(vehBrand)
	price = guiGetText(vehPrice)
	outputChatBox("Button Clicked")
	triggerServerEvent("senddSpawn", getLocalPlayer(), vehId, vehName, vehYear, vehBrand, vehPrice)
end

 

Server-Side:

Spoiler

--JMANIACS
local connection = exports.mysql:conexion() 


function spawnVeh(id, name, year, brand, price)
	IdInt = tonumber(id)
	local addVehicle = dbQuery(connection, "INSERT INTO `vehicles` (gtaid) VALUES (?)", IdInt)
	local result = dbPoll(addVehicle, -1)
		if result then
		outputChatBox("añadiendo")
		
		else
		outputChatBox("no añadiendo")
		end
end
addEvent("senddSpawn", true)
addEventHandler("senddSpawn", root, spawnVeh)

 

mysql-file:

Spoiler

--JMANIACS
resourceRoot = getResourceRootElement()

local datos = {
	host = "localhost",
	dbname = "testing",
	user = "root",
	password = "",
	port = "3306"
}

function conexion()

    db_Conexion = dbConnect ( "mysql", datos.dbname,datos.user, datos.password )
    return db_Conexion
end
function stop()
	if (isElement(db_Conexion)) then
		destroyElement(db_Conexion)
	end
end

addEventHandler("onResourceStop", resourceRoot, stop)

 

tabla mysql:

Spoiler

HYGNrSJ.png

 

Edited by Jmaniacs
Link to comment

Hola, estuve viendo tu código, lo corri en local y hay que cambiar varias cosas para que funcione.
Cliente-Side.lua tenias esta función:

function sendSpawn()
	id = guiGetText(vehId)
	name = guiGetText(vehName)
	year = guiGetText(vehYear)
	brand = guiGetText(vehBrand)
	price = guiGetText(vehPrice)
	outputChatBox("Button Clicked")
	triggerServerEvent("senddSpawn", getLocalPlayer(), vehId, vehName, vehYear, vehBrand, vehPrice)
end

En la parte de triggerServerEvent(....) estas queriendo enviarle los valores de las variables vehId, vehName,xxxx... Pero eso hace referencia al nombre del guiText en todo caso tendrías que usar guiGetText(NOMBRE DEL CAMPO).

La forma correcta seria la siguiente

function sendSpawn()
	id = guiGetText(vehId)
	name = guiGetText(vehName)
	year = guiGetText(vehYear)
	brand = guiGetText(vehBrand)
	price = guiGetText(vehPrice)
	outputChatBox("Button Clicked")
	triggerServerEvent("senddSpawn", getLocalPlayer(), id, brand, name, year, price)
end

Luego en el archivo server-side agrupe todos los códigos en uno (Para mayor comodida)

La conexión a la base la hace con

db_Conexion = dbConnect ( "mysql", datos.dbname,datos.user, datos.password )

Le faltan parámetros, la conexión correcta seria así

db_Conexion = dbConnect( "mysql", "dbname="..datos['dbname']..";host="..datos['host']..";charset=utf8", datos['user'], datos['password'])

El insert lo haces de la siguiente manera

local addVehicle = dbQuery(connection, "INSERT INTO `vehicles` (gtaid) VALUES (?)", IdInt)

Lo que haces ahí es solo "insertar" el id del vehículo. Te falta pasarle todos los otros campos y los datos correspondientes para insertar. Recorda que si queres omitir un campo lo tenes que marcar como nulo en la tabla. 

La manera correcta seria la siguiente

local addVehicle = dbQuery(connection, "INSERT INTO vehicles (gtaid,brand,name,year,price) VALUES (?,?,?,?,?)",IdInt,brand,name,year,price)

Otra cosa que vi son los outputchatbox, te falto agregar quien lo va a recibir, quedaría de la siguiente manera

outputChatBox("no añadiendo",source,255,255,255,true)

Reemplazando esas cosas que te dije debería funcionar. Yo me tome el atrevimiento y realice otros cambios como verificar si se conecto a la base de datos y otras mejoras. Te dejo mi Server-side.lua

resourceRoot = getResourceRootElement()

local datos = {
	host = "localhost",
	dbname = "test",
	user = "root",
	password = "",
	port = "3306"
}

local ConexionDB = dbConnect( "mysql", "dbname="..datos['dbname']..";host="..datos['host']..";charset=utf8", datos['user'], datos['password'])
if (not ConexionDB) then
    outputDebugString("Error: Failed to establish connection to the MySQL database server")
else
    outputDebugString("Success: Connected to the MySQL database server")
end

function spawnVeh(id,brand,name,year,price)
	IdInt = tonumber(id)
	local addVehicle = dbQuery(ConexionDB, "INSERT INTO vehicles (gtaid,brand,name,year,price) VALUES (?,?,?,?,?)",IdInt,brand,name,year,price)
	local result = dbPoll(addVehicle, -1)
	if result then
		outputChatBox("añadiendo",source,255,255,255,true)
	else
		outputChatBox("no añadiendo",source,255,255,255,true)
	end
end
addEvent("senddSpawn", true)
addEventHandler("senddSpawn", root, spawnVeh)

Cualquier consulta o duda mandame MP. Espero que te sirva Saludos :D

Edited by AndyGot
  • Thanks 1
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...