The first mistake is that `cache_insert_id` returns -1 on error for R40 version and above. You call a cache function before even executing the INSERT query.
The second mistake is that `format` function does not support %e specifier. Use `mysql_format` instead.
The third mistake is the number of INSERT queries you are trying to execute. It must be 1 query otherwise it will create 4 rows with different dbid but duplicate data.
Move the `IsPlayerInAnyVehicle` check at the very top.
You know the modelid, no need to store the vehicle name as it can be easily retrieved.
Vehicle owner should be an integer, sql unique id of the player which can be linked and can also be easily compared with. If a vehicle is not owned, owner should be NULL and makes `owned` column redundant.
Set default value for plate so you do not have to set it in the query. You call SetVehicleNumberPlate but the vehicle was not created yet.
Pass everything as arguments and when the query succeeded, assign them to the variables.
While INSERT INTO .. SET syntax is valid, I'll show an example with this mysql extension and not the standard sql syntax. If you change the owner datatype and remove the redundant vehicle name, you can remove them from the query:
pawn Код:
new player_interior = GetPlayerInterior(playerid),
player_virtual_world = GetPlayerVirtualWorld(playerid);
mysql_format(mysql, query, sizeof(query),
"INSERT INTO vehicles \
SET vehicleName='%e', \
vehicleOwner='-', \
vehiclePlate='SSRP', \
vehicleModel=%d, \
vehiclePrice=%d, \
vehicleColorOne=%d, \
vehicleColorTwo=%d, \
vehicleX=%f, \
vehicleY=%f, \
vehicleZ=%f, \
vehicleA=%f, \
vehicleInterior=%d, \
vehicleWorld=%d, \
vehicleOwned=0",
GetVehicleName(vehid),
vehid,
price,
color1,
color2,
x,
y,
z,
angle,
player_interior,
player_virtual_world);
mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", veh_index, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);
pawn Код:
forward OnVehicleCreate(veh_index, vehid, price, color1, color2, Float: x, Float: y, Float: z, Float: angle, vehicle_interior, vehicle_virtual_world);
public OnVehicleCreate(veh_index, vehid, price, color1, color2, Float: x, Float: y, Float: z, Float: angle, vehicle_interior, vehicle_virtual_world)
{
VehicleData[veh_index][vehicleDBID] = cache_insert_id();
VehicleData[veh_index][vehicleExists] = true;
VehicleData[veh_index][vehicleModel] = vehid;
strcpy(VehicleData[veh_index][vehicleName], GetVehicleName(vehid), MAX_PLAYER_NAME);
strcpy(VehicleData[veh_index][vehicleOwner], "-", MAX_PLAYER_NAME);
strcpy(VehicleData[veh_index][vehiclePlate], "SSRP", 16);
VehicleData[veh_index][vehiclePrice] = price;
VehicleData[veh_index][vehicleLock] = 0;
VehicleData[veh_index][vehicleColorOne] = color1;
VehicleData[veh_index][vehicleColorTwo] = color2;
VehicleData[veh_index][vehiclePos][0] = x;
VehicleData[veh_index][vehiclePos][1] = y;
VehicleData[veh_index][vehiclePos][2] = z;
VehicleData[veh_index][vehiclePos][3] = angle;
VehicleData[veh_index][vehicleInterior] = vehicle_interior;
VehicleData[veh_index][vehicleWorld] = vehicle_virtual_world;
VehicleData[veh_index][vehicleOwned] = 0;
/*
create the vehicle here.
what do `Vehicle_Save` and `Vehicle_Refresh` do?
*/
}