[HELP] Please help mysql id player
#1

Hello today I was register account and it was fine id of that account is 0 and then I was register my second account and id of that account it 0 but it should be 1 and for every player register acc id is go 0 1 2 3 4...What is the problem this is where insert on register


so this is register part


pawn Код:
new newid = mysql_fetch_int() + 1;
            new query[2500],hIme[MAX_PLAYER_NAME];
            RPIme(playerid,hIme);
            GetPlayerIp(playerid,PlayerInfo[playerid][IP],24);
            WP_Hash(PlayerInfo[playerid][Lozinka],129,inputtext);
            format(query,2500,"INSERT INTO `users` (`ID`,`Nick`,`Lozinka`,`Ip`,`Novac`,\
            `Registriran`,`SpawnPromijena`,`Spol`,`Godine`,`Porijeklo`,`Skin`,`Clan`,`Lider`,`Admin`,`GameSage`,`Utisan`,`MjestoAdmina`,`Rank`,`Banka`,`MjestoGS`,"
,query);
            format(query,2500,"%s `Kuca`,`Materijali`,`xDroga`,`hHealH`,`hArmourH`,`Banovan`,`Stan`,`MjestoUOrg`,`Donator`,`SatiIgranja`,`zDroga`,`MozeDaGasiMobitel`,`Respekti`,",query);
            format(query,2500,"%s `RacunMobitel`,`DozvolaOruzje`,`DozvolaLetenje`,`Kredit`,`Upozoren`,`Imenik`,`Zatvoren`,`ZatvorenVrijeme`,`DozvolaPlovidba`,",query);
            format(query,2500,"%s `TimUgovor`,`KaznaUgovor`) VALUES",query);
            format(query,2500,"%s ('%d','%s','%s','%s','600','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','-1','0','0','0','0','0','-1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0')",
            query,newid,hIme,PlayerInfo[playerid][Lozinka],PlayerInfo[playerid][IP],PlayerInfo[playerid][Novac]);
            mysql_query(query);
Reply
#2

Why dont you use auto_Increment in your database ?
Reply
#3

Use Auto-increment for the ID column and set ID as primary key as well.
Also set the default value to 0 for all columns that should have 0 when registering (or -1, like you have on 2 values).

You can setup such tables easily and you won't need such long register queries.
Then you only need to insert hIme,PlayerInfo[playerid][Lozinka],PlayerInfo[playerid][IP],PlayerInfo[playerid][Novac].
That's only 4 values. MySQL does the rest. It will automatically store a default value (which you set in the table-design) when inserting a new line without specifying any value for that column.

pawn Код:
format(Query, sizeof(Query), "CREATE TABLE `playerdata` (");
        format(Query, sizeof(Query), "%s`ID` int(11) NOT NULL AUTO_INCREMENT, ", Query);
        format(Query, sizeof(Query), "%s`PlayerName` varchar(25) NOT NULL, ", Query);
        format(Query, sizeof(Query), "%s`Password` varchar(130) NOT NULL, ", Query);
        format(Query, sizeof(Query), "%s`AdminLevel` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%s`Money` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%s`Score` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%s`Fines` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%s`Tickets` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%s`TicketCost` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%s`SpeedInMph` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%s`JailTime` int(11) NOT NULL DEFAULT '0', ", Query);
        format(Query, sizeof(Query), "%sPRIMARY KEY (`ID`)) ", Query);
        format(Query, sizeof(Query), "%sENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8", Query);
        mysql_query(SQL_db, Query, false);
You see in this example (it's code from my script to auto-create tables if they don't exist) that:
- ID is AUTO_INCREMENT and PRIMARY KEY
- all values from "AdminLevel" until "JailTime" have a default value of 0.

The only thing I need to register a new account is the player's name and password.
The ID is automatically set by MySQL to a unique value each time a player registers an account.
Name and password need to be inserted (as they cannot be NULL, by the "NOT NULL" declaration).
All the rest doesn't need to be specified because of the default values.
If they're not given, MySQL will copy the default value and set that value in the newly created account, for each column that has a default value specified.

My register query:
pawn Код:
// This function creates a new account for the given player
Player_AccountCreate(playerid)
{
    // Setup local variables
    new Query[256];

    // Add the player's account to the MySQL database (escape the name only, as there is no need to escape a hashed password)
    mysql_format(SQL_db, Query, sizeof(Query), "INSERT INTO playerdata (PlayerName, Password) VALUES ('%e', '%s')", APlayerData[playerid][PlayerName], APlayerData[playerid][PlayerPassword]);
    mysql_tquery(SQL_db, Query, "Player_OnAccountCreate", "i", playerid);
}

// This custom callback is used after creating a new account (playername and password are inserted, but the ID must be loaded now to reference the player later on using this ID)
forward Player_OnAccountCreate(playerid);
public Player_OnAccountCreate(playerid)
{
    // Get the ID from the auto-incremented table ("ID") and store it
    APlayerData[playerid][SQLID] = cache_insert_id();

    return 1;
}
Much shorter, just because of all the default values.
Reply
#4

Is that for will work and for vehicles for exxample one player buy one car then id of that car is 0 and second player buy second caar then the id of that car is 1


My account doesn't create and I don't use this cache get something
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)