Unban command - mysql
#1

Ok, well I have a rather weird bug that i'm not 100% sure about.

Since I'm new to mysql, i'll try to explain what I did.

What I wanted was a command where when a player is banned, it updates mysql with infos about the ban: player that was banned, admin that banned the player, the reason of ban, the activeness of the ban(either 1 or 0), and the user's IP. I finished this, and it updates to mysql nicely, while also banning the IP in samp.ban, which is what I wanted.

Now, here is my problem.

I have made an unban command. This command is supposed to navigate the database and find the IP of the player, unban them, and set the item 'active' to 0, meaning that they can connect. If it doesn't find the name, it will send a message letting them know that there is no player to unban (either doesn't exist, or the user is not banned.)

This is the code:
pawn Код:
dcmd_unban(playerid, params[])
{
    if(GetPlayerAdminLevel(playerid) < 1) return 0;
    new
        aName[24],
        str[256],
        string[256],
        name[100];

    GetPlayerName(playerid, aName, 24);

    if(sscanf(params, "z", name)) SendClientMessage(playerid, 0xFFFFFFFF, "[ @ ] Error! Use: /unban (name)");
    {
        format(str,sizeof(str),"SELECT ip FROM `bans` WHERE Banned='%s' AND active='1'",name);
        mysql_query(str);
        mysql_store_result();
        if(mysql_num_rows() == 0)
        {
            SendClientMessage(playerid,0xFFFFFFAF,"[ @ ] This player is not banned or doesn't exist!");
            mysql_free_result();
            return 1;
        }
        new check[1][50];
        mysql_fetch_row(string,"|");
        split(string,check,'|');
        mysql_free_result();
        format(string,sizeof(string),"unbanip %s",check[0]);
        SendRconCommand(string);
        format(str, 256, "[ @ ] %s has been unbanned by %s!", name,GetName(playerid));
        SendClientMessageToAll(0xFFFFFFAF, str);
        SendRconCommand("reloadbans");
        format(string,sizeof(string),"UPDATE `bans` SET active='0' WHERE Banned='%s' AND active='1'",name);
        mysql_query(string);
        printf("[UNBAN] %s has been unbanned by %s!", name, aName);
    }
    return true;
}
But what happens, is it always says that the player is not banned or does not exist, does not exist. Any reason why?

P.s. I tried using the sql syntax in wampserver, and it finds the player IP, however, when I use the unban command in game, nothing happens.

I hope I have given you enough information on the problem. Also, the ban command is below.

pawn Код:
dcmd_ban(playerid, params[])
{
    if(GetPlayerAdminLevel(playerid) < 1) return 0;
    new
        id,
        string[200],
        aName[24],
        msg[64];
    if(sscanf(params, "ds", id, msg)) SendClientMessage(playerid, 0xFF0000AA, "Usage: /ban <id> <reason>");
    if(!IsPlayerConnected(id)) return SendClientMessage(playerid,COLOR_RED,"[ ! ] Invalid playerid.");
    if(GetPlayerAdminLevel(id) >= GetPlayerAdminLevel(playerid)) return SendClientMessage(playerid,COLOR_RED,"[ ! ] You can't ban this player !");
//  if(id == playerid) return SendClientMessage(playerid,COLOR_RED,"[ ! ] You cannot ban yourself");
    else
    {
        GetPlayerName(playerid, aName, 24);
        format(msg, sizeof (msg), "%s%s !", msg[0] ? ("for ") : (""), msg);
        format(string, sizeof(string), "[ ! ] %s has been banned by %s : %s",GetName(id),aName,msg);
        SendClientMessageToAll(COLOR_AC,string);
        printf("[PLAYER-BANNED] NAME: %s / ADMIN: %s / REASON: %s",GetName(id),aName,msg);
        TogglePlayerControllable(id, 0);
        format(string, sizeof(string), "[ ! ] You have been banned from the server.");
        SendClientMessage(id,COLOR_RED,string);
        BanEx(id,msg);
    }
    return 1;
}
Reply
#2

Well there's no need for sscanf in that function at all since there is only one parameter, there's no need formatting or anything, you only need to escape the string!

pawn Код:
dcmd_unban(playerid, params[])
{
    if(GetPlayerAdminLevel(playerid) < 1) return 0;
    new
        aName[24],
        str[256],
        string[256],
        name[100];

    GetPlayerName(playerid, aName, 24);

    mysql_real_escape_string(params,name);

    format(str,sizeof(str),"SELECT ip FROM `bans` WHERE Banned='%s' AND active='1'",name);
    mysql_query(str);
    mysql_store_result();
    if(mysql_num_rows() == 0)
    {
        SendClientMessage(playerid,0xFFFFFFAF,"[ @ ] This player is not banned or doesn't exist!");
        mysql_free_result();
        return 1;
    }
    new check[1][50];
    mysql_fetch_row(string,"|");
    split(string,check,'|');
    mysql_free_result();
    format(string,sizeof(string),"unbanip %s",check[0]);
    SendRconCommand(string);
    format(str, 256, "[ @ ] %s has been unbanned by %s!", name,GetName(playerid));
    SendClientMessageToAll(0xFFFFFFAF, str);
    SendRconCommand("reloadbans");
    format(string,sizeof(string),"UPDATE `bans` SET active='0' WHERE Banned='%s' AND active='1'",name);
    mysql_query(string);
    printf("[UNBAN] %s has been unbanned by %s!", name, aName);
    return true;
}
Additionally I hope you do realize your string lengths are complete madness
Reply
#3

I wish I could say the above code worked, but unfortunately it didn't.

(btw, I know about the long strings, its only because mysql has a really long syntax, etc)

I don't really know what the problem is, and I even tried entering information into the sql part of PhpMyAdmin, and it shows the IP of a player, but heres another question.

Does mysql_num_rows, start off at 0, or at one? I'm not even sure now, since I had a friend script this for me. I'd ask him, but I can't seem to locate him right now.

Anyways, when I type /unban with that code, it says Unknown command, when the player name is entered and he or she is banned (Active is 1). And if I enter a player name that isn't banned (active is not 1) then it tells me the player is not banned or does not exist.

Does anyone know the solution to my problem?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)