Why do I get a duplicate entry error when there is no duplicate entry?
I'm getting the following error:
Duplicate entry 'ahamzaoui_v2' for key 'Username' in /opt/lampp/htdocs/webEchange/SiteWeb_V5/dupliquerCompte2.php on line 135
when running a php script. I've tried it many times but I don't know why I'm getting this error. Here is the code around line 135:
$sql = "UPDATE utilisateur SET Username = '" . mysql_real_escape_string($infos['Username']) . "2', MotDePasse = '" . mysql_real_escape_string($infos['MotDePasse']) . "2' WHERE Matricule = " . $infos['Matricule'];
line 135 -> if(mysql_query($sql))
as you can see, I'm trying to append a 2 to the existing value for Username. Here is what I don't understand. If I go have a look at the utilisateur table, I can't fi开发者_开发技巧nd any entry with the value ahamzaoui_v2.Then I try running the script, and I get the error! The funny thing is, after the script has run (and reported an error), the value is changed! I now have ahamzaoui_v2 instead of ahamzaoui_v. The problem is that the REST of the script is not executed so I only get a part of what I want (a small part).
Apart from a SELECT statement, this is the first query I run, so I really can't see how there can be a duplicate entry, espectially since I just checked for that before.
upon reading this thread, I tryed running check table utilisateur, but it revealed no errors whatsoever..
btw, if I run the script many times in a row, I always get the error, and the username ends up with a whole lot of 2's appended to it (ahamzaoui_v222)
Aside from a corrupted database or key index there's only one other thing I can think of: is the Matricule
field unique? Does SELECT * FROM utilisateur WHERE Matricule = '{$infos['Matricule']}'
return more than one row? If so, the UPDATE
statement will change the first row matched (hence the change in data), but updates to subsequent rows will fail and generate a warning (stopping script execution):
mysql> create table test (a INT UNIQUE, b INT );
Query OK, 0 rows affected (0.10 sec)
mysql> insert into test VALUES (NULL, 2);
Query OK, 1 row affected (0.03 sec)
mysql> insert into test VALUES (NULL, 2);
Query OK, 1 row affected (0.00 sec)
mysql> update test set a = 1 where b = 2;
ERROR 1062 (23000): Duplicate entry '1' for key 'a'
mysql> select * from test;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
| NULL | 2 |
+------+------+
2 rows in set (0.00 sec)
Does the table in question have a uniqueness constraint on the username column? Have you checked to see if there are duplicate values of the same name in the $infos array across your update iteration -- duplicate values that were not there in the database after you performed some type of string manipulation in php?
精彩评论