Bonjour à tous,
Pour un projet web où j'ai écrit un formulaire d'inscription, j'obtient une erreur apparemment dû à la contraintre de ma table TABLE2.
Voici mes codes:
Création des tables + insertion de quelques données
Code du formulaire d'ajoutCode:
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 DROP TABLE TABLE1, TABLE2; CREATE TABLE TABLE2 ( id INT AUTO_INCREMENT, libelle VARCHAR(255), PRIMARY KEY (id) ); CREATE TABLE TABLE1 ( id INT AUTO_INCREMENT, nom VARCHAR(255), prenom VARCHAR(255), age INT, libelleId INT, PRIMARY KEY (id), CONSTRAINT fk_table2_table1 FOREIGN KEY (id) REFERENCES TABLE2(id) ); INSERT INTO TABLE2 (id, libelle) VALUES (NULL, "Etudiant"), (NULL, "Chomage"), (NULL, "Artisan"), (NULL, "Ouvrier"), (NULL, "Bébé"); INSERT INTO TABLE1 (id, nom, prenom, age, libelleId) VALUES (NULL, "Mattone", "Thomas", 19, 1), (NULL, "Mattone", "Simon", 17, 4), (NULL, "Simoni", "Pierre", 19, 1), (NULL, "Fillette", "Beatrice", 37, 5), (NULL, "Gerges", "Max", 60, 2);
Code:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 <?php include "connexion_bdd.php"; ini_set('display_errors', 'on'); if (isset($_GET['id'])) { $id = $_GET['id']; $requete = "SELECT TABLE1.nom, TABLE1.prenom, TABLE1.age FROM TABLE1 WHERE TABLE1.id =".$id." "; $reponse = $bdd -> query($requete); $donnees = $reponse -> fetch(); } if (isset($_POST['nom']) and isset($_POST['prenom']) and isset($_POST['age'])) { $id = $_POST['id']; $nom = $_POST['nom']; $prenom = $_POST['prenom']; $age = $_POST['age']; $requete = "UPDATE TABLE1 SET nom = '".$nom."', prenom = '".$prenom."', age = ". $age ." WHERE TABLE1.id = ".$id; $bdd -> exec($requete); header("Location: Table1_show.php"); } ?> <?php include("v_head.php"); ?> <form class="form-horizontal" method="post" action="Table1_edit.php"> <input type="hidden" name="id" value="<?php if (isset($id)) { echo $id; }?>"> <div class="form-group"> <label>Nom</label> <input class="form-control" name="nom" type="text" size="18" value="<?= $donnees['nom']?>" /> </div> <div class="form-group"> <label>Prénom</label> <input class="form-control" name="prenom" type="text" size="18" value="<?= $donnees['prenom']?>"/> </div> <div class="form-group"> <label>Age</label> <input class="form-control" name="age" type="text" size="18" value="<?= $donnees['age']?>"/> </div> <button type="submit" name="submit" class="btn btn-primary">Modifier</button> <a href="Table1_show.php"><button type="button" class="btn btn-secondary">Retour</button></a> </form> <?php include "v_foot.php"?>
Je ne trouve pas d'erreur à première vue dans mes tables ou mon code, mais j'obtient cette erreur:
Si vous pouviez m'aider, merciCode:
1
2 INSERT INTO TABLE1(id, nom, prenom, age, libelleId) VALUES (NULL, 'Mattone', 'Zoé', 5, 5) Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`eval_BDD`.`TABLE1`, CONSTRAINT `fk_table2_table1` FOREIGN KEY (`id`) REFERENCES `TABLE2` (`id`)) in /home/thomas/Code/BDD/Modele_eval_machine/Table1_add.php:13 Stack trace: #0 /home/thomas/Code/BDD/Modele_eval_machine/Table1_add.php(13): PDO->exec('INSERT INTO TAB...') #1 {main} thrown in /home/thomas/Code/BDD/Modele_eval_machine/Table1_add.php on line 13