I have a table in my db. My table has several fields, including a field with an automatically increasing identifier specified as a primary key, and another field called a link, which I set as unique. To populate this table, I have a php script that inserts records into this table using pdo. Each time the insert was successful (this means that the “link” does not exist in the table), I am incrementing a variable called $ newOnes. If the value of 'reference' is already in the table, an exception is thrown with code 23000. In this case, I am incrementing another variable called $ doublons. Unfortunately, my script runs a fatal error with an exception of 23000 when the while loop processes the last table entry. And I do not understand. Thanks in advance for your help. Greetings. Mark.
My php code is:
try {
$connexion = connexion('localhost', 'user', 'user', 'mydb');
$connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$qry_bat = $connexion->query('SELECT...');
$ins_db = $connexion->prepare('INSERT...');
}
catch (PDOException $e) {
echo $e->getMessage();
}
while($row = $qry_bat->fetch(PDO::FETCH_ASSOC)) {
try {
$ins_db->execute(array(...));
$newOnes++;
}
catch (PDOException $e) {
if ($e->getCode() != 23000) {
echo '<span class="msg-alert">'.$e->getMessage().'</span>';
} else {
$doublons++;
}
}
}
Fatal error I get (note that line 22 refers to while (...) line):
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]:
Integrity constraint violation: 1062 Duplicate entry 'theFieldContentOfTheLastRecordOfTheTable' for key 'theFieldNameReference' in
/myFilePath/file.php:22 Stack trace: #0 /myFilePath/file.php(22): PDOStatement->fetch(2)
#1 {main} thrown in /myFilePath/file.php on line 22
EDIT //////////
original table (things to mention):
auto incremented id
table for insertion (things worth mentioning):
auto added to id field UNIQUE INDEX by reference
source
share