mysql vehicle system help
#1

I'm creating a mysql vehicle system and I've just came across a problem.

This is the problem, when a player buys a vehicle we call this:
pawn Код:
stock CreatePlayerVehicle(playerid, vehid, price)
{
    new string[256];
    format(string, sizeof(string), "%s", PlayerRPName(playerid));
    new Float:x, Float:y, Float:z, Float:a;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, a);
   
    mysql_format(MainPipeline, szQuery, sizeof(szQuery), "INSERT INTO `playercars` (`car_Owner`, `car_ModelID`, `car_X`, `car_Y`, `car_Z`, `car_A`) VALUES('%e', '%d', '%f', '%f', '%f', '%f')", string, vehid, x, y, z, a);
    mysql_function_query(MainPipeline, szQuery, true, "OnQueryFinish", "i", THREAD_CREATE_PVEH);
    return 1;
}
THREAD_CREATE_PVEH:
pawn Код:
case THREAD_CREATE_PVEH:
        {
            if(szRows)
            {
                new pid = cache_insert_id(), tmp[256];

                pVeh[pid][car_ID] = pid;

                cache_get_field_content(pid, "car_Owner", tmp);
                format(pVeh[pid][car_Owner], 256, "%s", tmp);

                cache_get_field_content(pid, "car_ModelID", tmp);
                pVeh[pid][car_ModelID] = strval(tmp);

                cache_get_field_content(pid, "car_Locked", tmp);
                pVeh[pid][car_Locked] = strval(tmp);

                cache_get_field_content(pid, "car_Color1", tmp);
                pVeh[pid][car_Color1] = strval(tmp);

                cache_get_field_content(pid, "car_Color2", tmp);
                pVeh[pid][car_Color2] = strval(tmp);

                cache_get_field_content(pid, "car_X", tmp);
                pVeh[pid][car_X] = floatstr(tmp);

                cache_get_field_content(pid, "car_Y", tmp);
                pVeh[pid][car_Y] = floatstr(tmp);

                cache_get_field_content(pid, "car_Z", tmp);
                pVeh[pid][car_Z] = floatstr(tmp);

                cache_get_field_content(pid, "car_A", tmp);
                pVeh[pid][car_A] = floatstr(tmp);

                cache_get_field_content(pid, "car_World", tmp);
                pVeh[pid][car_World] = strval(tmp);

                cache_get_field_content(pid, "car_Interior", tmp);
                pVeh[pid][car_Interior] = strval(tmp);

                cache_get_field_content(pid, "car_Plate", tmp);
                format(pVeh[pid][car_Plate], 32, "%s", tmp);

                cache_get_field_content(pid, "car_Cash", tmp);
                pVeh[pid][car_Cash] = strval(tmp);

                cache_get_field_content(pid, "car_Gun1", tmp);
                pVeh[pid][car_Gun1] = strval(tmp);

                cache_get_field_content(pid, "car_Gun2", tmp);
                pVeh[pid][car_Gun2] = strval(tmp);

                cache_get_field_content(pid, "car_Mats", tmp);
                pVeh[pid][car_Mats] = strval(tmp);

                cache_get_field_content(pid, "car_Cocaine", tmp);
                pVeh[pid][car_Cocaine] = strval(tmp);

                cache_get_field_content(pid, "car_Pot", tmp);
                pVeh[pid][car_Pot] = strval(tmp);

                cache_get_field_content(pid, "car_Mod1", tmp);
                pVeh[pid][car_Mod1] = strval(tmp);

                cache_get_field_content(pid, "car_Mod2", tmp);
                pVeh[pid][car_Mod2] = strval(tmp);

                cache_get_field_content(pid, "car_Mod3", tmp);
                pVeh[pid][car_Mod3] = strval(tmp);

                cache_get_field_content(pid, "car_Mod4", tmp);
                pVeh[pid][car_Mod4] = strval(tmp);

                cache_get_field_content(pid, "car_Mod5", tmp);
                pVeh[pid][car_Mod5] = strval(tmp);

                cache_get_field_content(pid, "car_Mod6", tmp);
                pVeh[pid][car_Mod6] = strval(tmp);

                cache_get_field_content(pid, "car_Mod7", tmp);
                pVeh[pid][car_Mod7] = strval(tmp);

                cache_get_field_content(pid, "car_Mod8", tmp);
                pVeh[pid][car_Mod8] = strval(tmp);

                cache_get_field_content(pid, "car_Mod9", tmp);
                pVeh[pid][car_Mod9] = strval(tmp);

                cache_get_field_content(pid, "car_Mod10", tmp);
                pVeh[pid][car_Mod10] = strval(tmp);

                cache_get_field_content(pid, "car_Mod11", tmp);
                pVeh[pid][car_Mod11] = strval(tmp);

                cache_get_field_content(pid, "car_Mod12", tmp);
                pVeh[pid][car_Mod12] = strval(tmp);

                cache_get_field_content(pid, "car_Mod13", tmp);
                pVeh[pid][car_Mod13] = strval(tmp);

                cache_get_field_content(pid, "car_Mod14", tmp);
                pVeh[pid][car_Mod14] = strval(tmp);

                cache_get_field_content(pid, "car_Mod15", tmp);
                pVeh[pid][car_Mod15] = strval(tmp);

                cache_get_field_content(pid, "car_Mod16", tmp);
                pVeh[pid][car_Mod16] = strval(tmp);

                cache_get_field_content(pid, "car_Mod17", tmp);
                pVeh[pid][car_Mod17] = strval(tmp);

                pVeh[pid][car_Vehicle] = AddStaticVehicleEx(pVeh[pid][car_ModelID], pVeh[pid][car_X], pVeh[pid][car_Y], pVeh[pid][car_Z], pVeh[pid][car_A], pVeh[pid][car_Color1], pVeh[pid][car_Color2], 10000);
                LinkVehicleToInterior(pVeh[pid][car_Vehicle], pVeh[pid][car_Interior]);
                SetVehicleVirtualWorld(pVeh[pid][car_Vehicle], pVeh[pid][car_World]);
                SavePlayerVehicle(pid);
            }
        }
SavePlayerVehicle:
pawn Код:
stock SavePlayerVehicle(vehicleid)
{
    mysql_format(MainPipeline, szQuery, sizeof(szQuery), "UPDATE `playercars` SET `car_Owner` = '%s', `car_ModelID` = '%d', `car_Locked` = '%d', `car_Color1` = '%d', `car_Color2` = '%d'", pVeh[vehicleid][car_Owner], pVeh[vehicleid][car_ModelID], pVeh[vehicleid][car_Locked], pVeh[vehicleid][car_Color1], pVeh[vehicleid][car_Color2]);
    mysql_format(MainPipeline, szQuery, sizeof(szQuery), "%s, `car_X` = '%f', `car_Y` = '%f', `car_Z` = '%f', `car_A` = '%f', `car_World` = '%d', `car_Interior` = '%d', `car_Plate` = '%s'", szQuery, pVeh[vehicleid][car_X], pVeh[vehicleid][car_Y], pVeh[vehicleid][car_Z], pVeh[vehicleid][car_A], pVeh[vehicleid][car_World], pVeh[vehicleid][car_Interior], pVeh[vehicleid][car_Plate]);
    mysql_format(MainPipeline, szQuery, sizeof(szQuery), "%s, `car_Cash` = '%d', `car_Gun1` = '%d', `car_Gun2` = '%d', `car_Mats` = '%d', `car_Cocaine` = '%d', `car_Pot` = '%d', `car_Mod1` = '%d', `car_Mod2` = '%d'", szQuery, pVeh[vehicleid][car_Cash], pVeh[vehicleid][car_Gun1], pVeh[vehicleid][car_Gun2], pVeh[vehicleid][car_Mats], pVeh[vehicleid][car_Cocaine], pVeh[vehicleid][car_Pot], pVeh[vehicleid][car_Mod1], pVeh[vehicleid][car_Mod2]);
    mysql_format(MainPipeline, szQuery, sizeof(szQuery), "%s, `car_Mod3` = '%d, `car_Mod4` = '%d', `car_Mod5` = '%d', `car_Mod6` = '%d', `car_Mod7` = '%d', `car_Mod8` = '%d', `car_Mod9` = '%d', `car_Mod10` = '%d'", szQuery, pVeh[vehicleid][car_Mod3], pVeh[vehicleid][car_Mod4], pVeh[vehicleid][car_Mod5], pVeh[vehicleid][car_Mod6], pVeh[vehicleid][car_Mod7], pVeh[vehicleid][car_Mod8], pVeh[vehicleid][car_Mod9], pVeh[vehicleid][car_Mod10]);
    mysql_format(MainPipeline, szQuery, sizeof(szQuery), "%s, `car_Mod11` = '%d', `car_Mod12` = '%d', `car_Mod13` = '%d', `car_Mod14` = '%d', `car_Mod15` = '%d', `car_Mod16` = '%d', `car_Mod17` = '%d' WHERE `car_ID` = '%d'", szQuery, pVeh[vehicleid][car_Mod11], pVeh[vehicleid][car_Mod12], pVeh[vehicleid][car_Mod13], pVeh[vehicleid][car_Mod14], pVeh[vehicleid][car_Mod15], pVeh[vehicleid][car_Mod16], pVeh[vehicleid][car_Mod17], vehicleid);
    mysql_function_query(MainPipeline, szQuery, true, "OnQueryFinish", "i", THREAD_NO_RESULT);
    return 1;
}
Problems with it:
1. The vehicle doesn't spawn
2. It will only allow to insert one row into the table, other wise it says Duplicate entry '0' for key 'PRIMARY'
Reply
#2

There are many bugs in this..
-Firstly make sure that id field is AUTO_INCREMENT
-AddStaticVehicleEx can only be used in OnGameModeInit, use CreateVehicle Instead..
-Why are you using cache_get_field_content in case THREAD_CREATE_PVEH.. cache_get_field_content will only work with SELECT type of query and case THREAD_CREATE_PVEH is being used with INSERT INTO type of query. You have to run another query from inside of case THREAD_CREATE_PVEH and put the cache_get_field_content code inside the callback which is called from it.
Reply
#3

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
There are many bugs in this..
-Firstly make sure that id field is AUTO_INCREMENT
-AddStaticVehicleEx can only be used in OnGameModeInit, use CreateVehicle Instead..
-Why are you using cache_get_field_content in case THREAD_CREATE_PVEH.. cache_get_field_content will only work with SELECT type of query and case THREAD_CREATE_PVEH is being used with INSERT INTO type of query. You have to run another query from inside of case THREAD_CREATE_PVEH and put the cache_get_field_content code inside the callback which is called from it.
Yeah right, I understand what you're saying about needing to use SELECT. So, how would I do that and select the row we just put in? So I want to insert the new data, and then grab the car_ID from that newly created row to use straight away.

So
1. Insert data
2. Grab car_ID from that newly created row
3. Use data straight away
Reply
#4

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
So
1. Insert data
2. Grab car_ID from that newly created row
3. Use data straight away
Yes, use the car id in the select query.
Reply
#5

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
Yes, use the car id in the select query.
Can you show me an example of how? I'm confused and stuck on how to straight after inserting the data and selecting that specific data
Reply
#6

Learn how to split your data into several tables. More rows less columns.
https://sampforum.blast.hk/showthread.php?tid=420363
Reply
#7

Edit: all working thanks to both of you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)