SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 01/01/2019, 12:08 PM   #1
SkyFlare
Huge Clucker
 
SkyFlare's Avatar
 
Join Date: Apr 2015
Location: NZ
Posts: 221
Reputation: 43
Default MySQL not saving new row

So I have been scripting a Marijuana plant system..
I can create the plant successfully, it spawns, all data showing correctly....
when I delete it, it deletes correctly, but it hasnt saved/deleted from MySQL at all during this process

pawn Code:
stock Create_Weed(playerid, weedid, Float:x, Float:y, Float:z)
{
    new createweedstockstring[150];
    GetPlayerPos(playerid, x, y, z);
    WeedPlants[weedid][Exists] = 1;
    WeedPlants[weedid][ID] = cache_insert_id();
    WeedPlants[weedid][Planter] = GetPlayerNameEx(playerid);
    WeedPlants[weedid][PosX] = x;
    WeedPlants[weedid][PosY] = y;
    WeedPlants[weedid][PosZ] = z -1.0;
    WeedPlants[weedid][InteriorID] = GetPlayerInterior(playerid);
    WeedPlants[weedid][WorldID] = GetPlayerVirtualWorld(playerid);
    WeedPlants[weedid][NameTextLabel] = Text3D:INVALID_3DTEXT_ID;
    WeedPlants[weedid][Object] = -1;
    WeedPlants[weedid][Buds] = 20;
    if(WeedPlants[weedid][Exists])
    {
        DestroyDynamic3DTextLabel(WeedPlants[weedid][NameTextLabel]);
        DestroyObject(WeedPlants[weedid][Object]);
        new stringz[90];
        format(stringz, sizeof(stringz), "[%i] {33CCFF}Marijuana Plant | %i Buds | Planter: %s{FFFFFF}", weedid, WeedPlants[weedid][Buds], WeedPlants[weedid][Planter]);
        WeedPlants[weedid][NameTextLabel] = CreateDynamic3DTextLabel(stringz, -1, x, y, z, 5.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, WeedPlants[weedid][WorldID], WeedPlants[weedid][InteriorID], -1, 20.0);
        WeedPlants[weedid][Object] = CreateObject(3409, x, y, z-5.0, 0.0, 0.0, 0.0, 50.0);
        format(createweedstockstring,sizeof(createweedstockstring), "SERVER: {ffff00}%i Marijuana Plant{ffffff} created successfully!",weedid);
        SendClientMessage(playerid, -1, createweedstockstring);
        mysql_format(Database, queryForm, sizeof(queryForm), "INSERT INTO weedplants (id, x, y, z, worldid, interiorid, buds, planter) VALUES(%i, '%f', '%f', '%f', %i, %i, %i, %s)", WeedPlants[weedid][ID], x, y, z, WeedPlants[weedid][WorldID], WeedPlants[weedid][InteriorID], WeedPlants[weedid][Buds], WeedPlants[weedid][Planter]);
        mysql_tquery(Database, queryForm);
        mysql_format(Database, queryForm, sizeof(queryForm), "UPDATE weedplants SET world = %i WHERE id = %i", WeedPlants[weedid][WorldID], WeedPlants[weedid][ID]);
        mysql_tquery(Database, queryForm);
    }
    return 1;
}

pawn Code:
CMD:removeweed(playerid, params[])
{
    if(Player[playerid][AdminLevel] >= 3)//Head Administrator
    {
        new weedid, string[150];

        if(sscanf(params, "i", weedid))
        {
            return SendClientMessage(playerid, -1, "SERVER: /removeweed [weedid]");
        }
        if(!(0 <= weedid < MAX_WEEDPLANTS) || !WeedPlants[weedid][Exists])
        {
            return SendClientMessage(playerid, -1, "SERVER: That Marijuana Plant ID Does not exist.");
        }

        DestroyDynamic3DTextLabel(WeedPlants[weedid][NameTextLabel]);
        DestroyObject(WeedPlants[weedid][Object]);

        mysql_format(Database, queryForm, sizeof(queryForm), "DELETE FROM weedplants WHERE id = %i", WeedPlants[weedid][ID]);
        mysql_tquery(Database, queryForm);
        WeedPlants[weedid][Exists] = 0;
        WeedPlants[weedid][ID] = 0;
        WeedPlants[weedid][PosX] = 0;
        WeedPlants[weedid][Planter] = -1;
        WeedPlants[weedid][ID] = cache_insert_id();
        WeedPlants[weedid][PosX] = 0;
        WeedPlants[weedid][PosY] = 0;
        WeedPlants[weedid][PosZ] = 0;
        WeedPlants[weedid][InteriorID] = 999999;
        WeedPlants[weedid][WorldID] = 999999;
        WeedPlants[weedid][NameTextLabel] = Text3D:INVALID_3DTEXT_ID;
        WeedPlants[weedid][Object] = -1;

        format(string,sizeof(string),"SERVER: You have removed Marijuana Plant ID {ffff00}%i",weedid);
        SendClientMessage(playerid, -1, string);
    }
    else
    {
        SendClientMessage(playerid, -1, "SERVER: You are not authorized to use that command!");
    }
    return 1;
}

Very very confused as it looks like it should be good to go for saving MYSQL, any help or advice would be appreciated thanks
__________________
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
Link
Information
S-Store System V2 LinkIngame Store in Dialogs
SkyFlare is offline   Reply With Quote
Old 01/01/2019, 01:43 PM   #2
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: MySQL not saving new row

Why do you need two queries? An INSERT is enough to insert the correct world. For some reason you have two columns `worldid` and `world` according to your code.

Second important note is that you may execute a query but you cannot be certain it was successful. This is the wrong way:
pawn Code:
mysql_tquery(..., "DELETE ...");
WeedPlants[weedid][...] = 0;
If the query fails for whatever reason, you destroyed them in-game but not on database. The correct way is:
pawn Code:
mysql_tquery(..., "DELETE ...", "OnWeedPlantsDeleted", "d", weedid);
pawn Code:
forward OnWeedPlantsDeleted(weedid);
public OnWeedPlantsDeleted(weedid)
{
    WeedPlants[weedid][...] = 0;
}

This is also wrong:
pawn Code:
WeedPlants[weedid][ID] = cache_insert_id();
No active cache.
Calisthenics is offline   Reply With Quote
Old 01/01/2019, 02:21 PM   #3
Banditul18
Huge Clucker
 
Join Date: Nov 2014
Location: Brasov,Romania
Posts: 408
Reputation: 32
Default Re: MySQL not saving new row

^^ Also this
PHP Code:
%
should be
PHP Code:
'%s' 
in your mysql_format for any type of query
Banditul18 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[MYSQL]-HARDCODING - Loading , Saving The player name from mysql PURG3D Scripting Help 8 06/09/2016 08:12 AM
saving mysql iBots Scripting Help 2 17/04/2015 03:52 PM
Converting MYSQL Saving Codes into .INI Saving System codes ???? PURG3D Scripting Help 6 14/04/2015 10:49 PM
MySQL saving system to DJSON saving system TonyNames Scripting Help 4 21/01/2014 06:27 PM
saving system mysql(not mysql problem) martoivanov Scripting Help 0 29/08/2013 03:37 PM


All times are GMT. The time now is 07:30 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.