SQL Error?
#1

I am trying to add a vehicle plate to cars, but it isn't working properly. It is saving 5 characters to 'carPlate' in the database, but it is also setting the values of 'carLocked', 'carParked' and 'carWorld' to random numbers between 10-99. I can't figure out why it's doing this, it was working just fine before but now, all of a sudden, it's setting stuff it shouldn't be.

This is the function which creates cars and sets its values...
PHP код:
PlayerCar_Create(owneridmodelidFloat:xFloat:yFloat:zFloat:anglecolor1color2)
{
    for (new 
0!= MAX_DYNAMIC_CARS++)
    {
        if (!
CarData[i][carExists])
           {
               if (
color1 == -1)
                   
color1 random(127);
            if (
color2 == -1)
                
color2 random(127);
               
CarData[i][carExists] = true;
            
CarData[i][carModel] = modelid;
            
CarData[i][carOwner] = ownerid;
            
CarData[i][carPos][0] = x;
            
CarData[i][carPos][1] = y;
            
CarData[i][carPos][2] = z;
            
CarData[i][carPos][3] = angle;
            
CarData[i][carColor1] = color1;
            
CarData[i][carColor2] = color2;
            
CarData[i][carPaintjob] = -1;
            
CarData[i][carLocked] = false;
            
CarData[i][carParked] = 0;
            
CarData[i][carWorld] = 0;
            
CarData[i][carImpounded] = -1;
            
CarData[i][carImpoundPrice] = 0;
            
            new 
str[8];
            
format(strsizeof(str), "%s%s%d%d%s%s%s"LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], random(10), random(10), LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))]);
            
SetVehicleNumberPlate(istr);
            
format(CarData[i][carPlate], 11"%s"str);
        
            
mysql_function_query(g_iHandle"INSERT INTO `playercars` (`carModel`) VALUES(0)"false"OnPlayerCarCreated""d"i);
            return 
i;
        }
    }
    return -
1;
}
forward OnPlayerCarCreated(carid);
public 
OnPlayerCarCreated(carid)
{
    if (
carid == -|| !CarData[carid][carExists])
        return 
0;
    
CarData[carid][carID] = mysql_insert_id();
    
PlayerCar_Save(carid);
    return 
1;

It's inserting everything into the database fine, except the plate... here's a screenshot...


Am I using INSERT INTO wrong?
Reply
#2

Use %c to print a single character instead of %s. Format works in a quite mysterious way when you specify a string with an index, which may not always be understood. Basically it starts the string at the specified but it also writes the rest of the string. So if you have your string "ABCDEFGH..." and you access string[2] it will output "CDEFGH..." instead of just 'C'. I think it also overwrites the null terminator in the buffer. This causes buffer overflow which causes other variables to be overwritten inadvertently; 55 happens to be the character code for the character '7' and 80 is the character code for the character 'P'.
Reply
#3

Thank you for the reply!

I've modified my code accordingly (I think...) --

PHP код:
PlayerCar_Create(owneridmodelidFloat:xFloat:yFloat:zFloat:anglecolor1color2)
{
    for (new 
0!= MAX_DYNAMIC_CARS++)
    {
        if (!
CarData[i][carExists])
           {
               if (
color1 == -1)
                   
color1 random(127);
            if (
color2 == -1)
                
color2 random(127);
               
CarData[i][carExists] = true;
            
CarData[i][carModel] = modelid;
            
CarData[i][carOwner] = ownerid;
            
CarData[i][carPos][0] = x;
            
CarData[i][carPos][1] = y;
            
CarData[i][carPos][2] = z;
            
CarData[i][carPos][3] = angle;
            
CarData[i][carColor1] = color1;
            
CarData[i][carColor2] = color2;
            
            new 
str[8], query[400];
            
format(strsizeof(str), "%c%c%d%d%c%c%c"LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], random(10), random(10), LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))], LetterList[random(sizeof(LetterList))]);
            
SetVehicleNumberPlate(istr);
            
format(CarData[i][carPlate], 11"%s"str);
            
            
CarData[i][carPaintjob] = -1;
            
CarData[i][carLocked] = false;
            
CarData[i][carParked] = 0;
            
CarData[i][carWorld] = 0;
            
CarData[i][carImpounded] = -1;
            
CarData[i][carImpoundPrice] = 0;
        
            for (new 
014++)
            {
                if (
5)
                {
                    
CarData[i][carWeapons][j] = 0;
                    
CarData[i][carAmmo][j] = 0;
                }
                
CarData[i][carMods][j] = 0;
            }
            
CarData[i][carVehicle] = CreateVehicle(modelidxyzanglecolor1color2, -1);
            if (
CarData[i][carVehicle] != INVALID_VEHICLE_ID) {
                
ResetVehicle(CarData[i][carVehicle]);
            }
            
//mysql_function_query(g_iHandle, "INSERT INTO `playercars` (`carModel`) VALUES(0)", false, "OnPlayerCarCreated", "d", i);
            
mysql_format(g_iHandlequerysizeof(query), "INSERT INTO `playercars` (carModel, carOwner, carPosX, carPosY, carPosZ, carPosR, carColor1, carColor2, carPlate, carPaintjob, carParked, carWorld) VALUES(%d, %d, %f, %f, %f, %f, %d, %d, '%s', -1, 0, 0)",
                
CarData[i][carModel],
                
CarData[i][carOwner],
                
CarData[i][carPos][0],
                
CarData[i][carPos][1],
                
CarData[i][carPos][2],
                
CarData[i][carPos][3],
                
CarData[i][carColor1],
                
CarData[i][carColor2],
                
CarData[i][carPlate],
                
CarData[i][carPaintjob],
                
CarData[i][carParked],
                
CarData[i][carWorld]);
            
mysql_tquery(g_iHandlequery"OnplayerCarCreated""d"i);
            return 
1;
        }
    }
    return -
1;

and I'm also inserting the values in one by one. This has fixed the issue with the Plate setting the Locked/World/Parked values - they now remain at 0, which is their default, however the Plate is still not working, it is saving into the database like this...

Reply
#4

Hm, I've been playing around with this and no matter what I do, it's saving them like this...





or variants thereof. I've tried with both %c and %s.
Reply
#5

If you check IG, is the CarPlate being to the correct text?

Or is that also giving those weird texts
Reply
#6

The car plate in game is displaying the carPlate SQL column, with the strange characters
Reply
#7

Anyone?
Reply
#8

Stupid question maybe, but what is the actual size of the plate array? First you define it as 8 then as 11 but what is its actual definition?
Reply
#9

Quote:
Originally Posted by adamslj
Посмотреть сообщение
The car plate in game is displaying the carPlate SQL column, with the strange characters
Does this also happen when you call that code from IG, while you create a new car?
If so, i'm starting to think that this is what it generates, you can try printing the "result" of the random part in the console, and see what it outputs, to see if it even generates a correct plate number
Reply
#10

Quote:
Originally Posted by Vince
Посмотреть сообщение
Stupid question maybe, but what is the actual size of the plate array? First you define it as 8 then as 11 but what is its actual definition?
I didn't define an array in the enum, I've just realised that now... that's probably what's going on. The SQL table is varchar(11) so would the array be 11, or 12?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)