OnSpawn execute SQL request

Hello I search to change a value in mysql table with a lua file, but it doesn’t work, someone can help me please ?
my code:

 AddEventHandler("playerSpawned", function(spawn)
  TriggerEvent('es:getPlayerFromId', source, function(user)
    local veh_id = veh_id
    local player = user.identifier
    local executed_query = MySQL:executeQuery("SELECT * FROM user_vehicle WHERE identifier = '@username' AND ID = '@ID'",{['@username'] = player, ['@ID'] = veh_id})
    local result = MySQL:getResults(executed_query, {'vehicle_model', 'vehicle_plate', 'vehicle_state', 'vehicle_colorprimary', 'vehicle_colorsecondary', 'vehicle_pearlescentcolor', 'vehicle_wheelcolor' }, "identifier")
    if(result)then
      local executed_query = MySQL:executeQuery("UPDATE user_vehicle SET vehicle_state='Rentré' WHERE identifier='@username'",{['@username'] = player, ['@ID'] = veh_id})
    end


    --Notification
    Citizen.CreateThread(function()
    while true do
      Wait(0)
     
      SetNotificationTextEntry("STRING");
      AddTextComponentString("Bienvenue sur ~b~Identity Roleplay!");
      SetNotificationMessage("CHAR_ALL_PLAYERS_CONF", "CHAR_ALL_PLAYERS_CONF", true, 1, "Identity roleplay", "v0.2");
      DrawNotification(false, true);
      Wait(10000000)
    end
  end)
 end)

I search to change the value in vehicle_state to ‘Rentré’ automatically, but I’m not good in lua so, I don’t understand my error :confused:

1 Like

Replace your query with this:

local executed_query = MySQL:executeQuery("UPDATE user_vehicle SET vehicle_state='Rentré' WHERE identifier='@username'", {['@username'] = player})

Look at the differences between mine and your code. You was trying to use the vehicle id when it wasn’t needed.

See if that works.

Hello Cloud, I have try your code on my server, but same result :confused:
Thanks for your help anyways

I’ve only just noticed the rest of the code.

local veh_id = veh_id

Where are you getting veh_id from because that’s currently doing nothing of interest.

I’m trying to get the veh_id from my table users_vehicle.
I have start lua since 3 days ago, it’s really new for me :confused:

Thank you Cloud :smiley: thanks to ur observation I have find my error :smiley:

AddEventHandler('playerDropped', function()
	if(Users[source])then
        MySQL:executeQuery("UPDATE user_vehicle SET vehicle_state='Rentré' WHERE identifier='@username'", {['@username'] = Users[source].identifier})
		Users[source] = nil
	end
end)