Command
#1

Can someone tell me why this command doesn't work in the way it should? Basically, if i create a faction, it's ID is 1, but if i create another one, it's ID is again 1 and replaces the first faction. Also sometimes doesn't write anything in the table. P.S It's in sampdb, not MySQL:

pawn Код:
CMD:fcreate(playerid,params[])
{
    if (Player[playerid][AccountLevel] < 5) return 0;

    if(IsPlayerInFaction(playerid)) return SCM(playerid, COLOR_LIGHTRED, "* You're already in a faction. Type /fleave to quit.");

    new query[115], DBResult: result, string[128];

    if(isnull(params)) return SCM(playerid, -1, "{3498db}* [USAGE]: {FFFFFF} /fcreate [faction name]");
    if(!strcmp(params,"INVALID",true)) return SCM(playerid, COLOR_LIGHTRED, "* Chosen faction name is invalid.");

    format(query,sizeof(query),"SELECT FactionName FROM Factions WHERE FactionName = '%q'",params);
    result = db_query(Database, query);

    if(db_num_rows(result))
    {
        db_free_result(result);
        return SCM(playerid, COLOR_LIGHTRED, "* This faction name already has been taken.");
    }
    db_free_result(result);

    /*FactionInfo[playerid][factionmember] = 1;
    format(FactionInfo[playerid][factionname], 32, params);
    FactionInfo[playerid][factionleader] = 1;*/


    ShowPlayerDialog(playerid,DIALOG_FACTION_COLOR,DIALOG_STYLE_LIST,"Faction Color",""BLUE"Blue\n"RED"RED\n"WHITE"White\n"PINK"Pink\n"CYAN"Cyan\n"ORANGE"Orange\n"GREEN"Green\n"YELLOW"Yellow","Select","Cancel");

    new Query[217];
    format( Query, sizeof(Query), "INSERT INTO Factions (FactionName,FactionColor) VALUES ('%q','%q')", FactionInfo[playerid][factionname],FactionInfo[playerid][factioncolor]);
    db_query(Database,Query);
    result = db_query(Database, "SELECT last_insert_rowid()");
    FactionInfo[playerid][factionid] = db_get_field_int(result);
    db_free_result(result);

    /*format( Query,sizeof(Query), "INSERT INTO Members (FactionID,UserName,FactionLeader) VALUES (%d,'%q',1)",FactionInfo[playerid][factionid],FactionInfo[playerid][username]);
    db_query(Database,Query);*/


    SCM(playerid, COLOR_YELLOW, "* You have sucessfully created a faction!");

    format(string,sizeof(string),""GREEN"%s "WHITE"has created a new faction named "GREEN"%s!"WHITE"",FactionInfo[playerid][username],FactionInfo[playerid][factionname]);
    SCMTA(-1,string);
    return 1;
}
Reply
#2

First, you should end your command till here:
pawn Код:
ShowPlayerDialog(playerid,DIALOG_FACTION_COLOR,DIALOG_STYLE_LIST,"Faction Color",""BLUE"Blue\n"RED"RED\n"WHITE"White\n"PINK"Pink\n"CYAN"Cyan\n"ORANGE"Orange\n"GREEN"Green\n"YELLOW"Yellow","Select","Cancel");
Because you don't have the color for the faction yet, so your INSERT query may not be right. The INSERT query part should be done under "DIALOG_FACTION_COLOR".

I don't know if SAMP's SQLite is up-to-date for running this "last_insert_rowid()" but apart from that query looks wrong (i have to say i haven't quiet used SQLite in a while), there is no table name mentioned in your query so i guess it will give you a syntax error!

Here's how i would do it:
pawn Код:
format(Query, sizeof(Query), "SELECT id FROM Factions WHERE FactionName = '%q' LIMIT 1", FactionInfo[playerid][factionname]);
result = db_query(Database, Query);

if (result == DBResult:0 || db_num_rows(result) == 0) {
    // INSERT statement didn't work so the faction wasn't created
    return 1;
}

FactionInfo[playerid][factionid] = db_get_field_int(result);

db_free_result(result);
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)