Duplicate Entry for ID key (Register/Mysql)
#1

Basically, I'm making a new server using Mysql for the first time. As can be expected, I've had a few issues along the way. I followed a guide from the forums, but it seems that whenever a player tries to register (I registered once successfully, taking up ID 0), they try to submit their ID as 0.

Below is my code which holds the player data, and the code which (should) properly save each new player as they register with the next ID available in the primary row.

Code:
enum PlayerData
{
    ID,
    Name[MAX_PLAYER_NAME],
    Password[129],
    IP[16],
    Admin,
    VIP,
    Money,
    Float:posX,
    Float:posY,
    Float:posZ,
    Float:posA
};
new Player[MAX_PLAYERS][PlayerData];
Code:
forward OnAccountRegister(playerid);
public OnAccountRegister(playerid)
{
    Player[playerid][ID] = cache_insert_id();
    printf("[Registration] New account registered. Database ID: [%d]", Player[playerid][ID]);

    TogglePlayerSpectating(playerid, false);

    SetSpawnInfo(playerid, 0, 23, SPAWN_X, SPAWN_Y, SPAWN_Z, SPAWN_A, 0, 0, 0, 0, 0, 0);
    SpawnPlayer(playerid);
    return true;
}
Code:
CMySQLQuery::Execute[OnAccountRegister(i)] - (error #1062) Duplicate entry '0' for key 'PRIMARY'


Hopefully this is enough for you all to get an idea of the issue I am having, if not, let me know and I'll provide any information you need. I am stumped, I've tried searching around for this problem but I can't seem to find it anywhere.
Reply
#2

Set auto increment for "ID" column so it will generate itself the next available (and unique at the same time) value.

When a player registers and you send an INSERT query, either specify ID as NULL or don't have it at all.
Reply
#3

Thanks bro, still learning all of this MySQL information as I go, fixed my problem with your help.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)