Problem with MySQL insert ID
#1

Hey, i have a little problem with MySQL insert ID in last days. When i want to create dynamic factions or buy a car or create a business, then it doesnt. When i check debug, there is an error. Unfortunately, MySQL is my weakness, so i apperciate some help. I have tried to fix this a lot of hour but no result...
pawn Код:
An error has occured. (Error ID: 1062, Duplicate entry '2' for key 'PRIMARY')
pawn Код:
CMD:teefraktsioon(playerid, params[])
{
    new query[400], fname[32], puudub[32];
    if(sscanf(params, "s[32]", fname)) return SendClientMessage(playerid, V_PUNANE, "KASUTA: /teefraktsioon [nimi]");
    mysql_store_result();
    new id = mysql_insert_id();
    mysql_free_result();
    SendClientMessage(playerid, COLOR_LIGHTORANGE, "Fraktsioon edukalt loodud");
    format(query, sizeof(query),"INSERT INTO factions(ID, Name, Rank1, Rank2, Rank3, Rank4, Rank5) VALUES(%d, '%s', 'puudub', 'puudub', 'puudub', 'puudub', 'puudub')", id, fname);
    mysql_query(query);
    fInfo[id][frID] = id;
    fInfo[id][Name] = fname;
    fInfo[id][Rank1] = puudub;
    fInfo[id][Rank2] = puudub;
    fInfo[id][Rank3] = puudub;
    fInfo[id][Rank4] = puudub;
    fInfo[id][Rank5] = puudub;
    return 1;
}
Reply
#2

You're inserting "id" into factions, don't do that, it should set it itself, I think.. Is ID set to AI in the SQL DB?
Reply
#3

Yes, ID is set AI
Reply
#4

Try removing the insertion again, don't reinsert it into the database.
Reply
#5

If you have auto increment set for your ID in your database then every entry should increment itself automatically. I always set the ID to primary as well. What "Error ID: 1062, Duplicate entry '2' for key 'PRIMARY'" means is that you are trying to insert something in a row that has data already. If you want to update a row then use the UPDATE statement and use it carefully because if you write the query wrong and execute it, it will mess up your whole table.
Here's how to update a busy row:
pawn Код:
new query[128];
format(query, sizeof(query),"UPDATE `groceries` SET `potatoes` = 10 WHERE ID = 1");
mysql_query(query);
That would update the amount of potatoes to 10 in the first row. The table would be groceries, "Potatoes" in this case would be a column.
Also make sure to escape everything so you don't get your database injected. I like to think of the sea battle game or Microsoft Excel when I work with SQL queries.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)