Bonjour,
J'ai la fonction PHP suivante:
et la structure de table de base de données suivante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 function insertPerson(array $data): string|false { $db = dbConnect(); $sql = <<<SQL INSERT IGNORE INTO dat_persons (id_abo, gender, lastname, firstname, middlename, birthyear, birthmonth, birthday, birthplace, christeningyear, christeningmonth, christeningday, deathyear, deathmonth, deathday, deathplace, profession, comment, id_father, id_mother, birthorder) VALUES(:id_abo, :gender, :lastname, :firstname, :middlename, :birthyear, :birthmonth, :birthday, :birthplace, :christeningyear, :christeningmonth, :christeningday, :deathyear, :deathmonth, :deathday, :deathplace, :profession, :comment, :id_father, :id_mother, :birthorder) SQL; $stmt = $db->prepare($sql); $stmt->execute($data); return $db->lastInsertId(); }Lorsque j'insère plusieurs fois un jeu de données, l'enregistrement est dupliqué où seule la date de mise à jour varie. Comment empêcher cet enregistrement?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 CREATE TABLE IF NOT EXISTS `dat_persons` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `id_abo` int(10) UNSIGNED NOT NULL, `gender` char(1) NOT NULL, `lastname` varchar(30) NOT NULL, `firstname` varchar(30) NOT NULL, `middlename` varchar(45) DEFAULT NULL COMMENT 'autres prénoms', `birthyear` char(4) DEFAULT NULL, `birthmonth` char(2) DEFAULT NULL, `birthday` char(2) DEFAULT NULL, `birth_date` date DEFAULT NULL, `birthplace` varchar(40) DEFAULT NULL, `christeningyear` char(4) DEFAULT NULL, `christeningmonth` char(2) DEFAULT NULL, `christeningday` char(2) DEFAULT NULL, `christening_date` date DEFAULT NULL COMMENT 'date de baptême', `deathyear` char(4) DEFAULT NULL, `deathmonth` char(2) DEFAULT NULL, `deathday` char(2) DEFAULT NULL, `death_date` date DEFAULT NULL, `deathplace` varchar(40) DEFAULT NULL, `profession` varchar(60) DEFAULT NULL, `comment` tinytext COMMENT 'annotations', `id_father` int(10) UNSIGNED DEFAULT NULL COMMENT 'clefPere', `id_mother` int(10) UNSIGNED DEFAULT NULL COMMENT 'clefMere', `birthorder` tinyint(2) DEFAULT NULL COMMENT 'ordre_dans_fratrie', `selected` tinyint(1) DEFAULT NULL, `create_date` datetime DEFAULT CURRENT_TIMESTAMP, `update_date` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `person` (`id_abo`,`lastname`,`firstname`,`birth_date`,`id_mother`), KEY `father_id` (`id_father`), KEY `mother_id` (`id_mother`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Partager