Insertion de données provenant de +ieurs tabl
monaa
-
Dalida Messages postés 7114 Date d'inscription Statut Contributeur Dernière intervention -
Dalida Messages postés 7114 Date d'inscription Statut Contributeur Dernière intervention -
bjr a ts
je voudrais insérer des données ds plusieurs tables mysql a partir d'un formulaire , j'ai vu un exemple sur le net , c'est fesable uniquement pour 2 tables pas +, je coince pr insrer ts mes données , avez vs des exemples ou des liens web évocant un exemple avec +ieurs tables liés par des contraintes d'intégrité, la techno PDO serait elle + intéressente que de travailler normalement ??
merçi a ts
je voudrais insérer des données ds plusieurs tables mysql a partir d'un formulaire , j'ai vu un exemple sur le net , c'est fesable uniquement pour 2 tables pas +, je coince pr insrer ts mes données , avez vs des exemples ou des liens web évocant un exemple avec +ieurs tables liés par des contraintes d'intégrité, la techno PDO serait elle + intéressente que de travailler normalement ??
merçi a ts
A voir également:
- Insertion de données provenant de +ieurs tabl
- Fuite données maif - Guide
- Insertion table des matières word - Guide
- Touche insertion clavier - Guide
- Supprimer les données de navigation - Guide
- Insertion video powerpoint - Guide
5 réponses
je ne suis pas sur d'avoir compris ta question mais voila ...
t'en exécute plusieurs à la suite:
https://www.php.net/manual/fr/function.mysql-query.php
exemple :
<?
mysql_query ('INSERT INTO `table1` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table2` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table3` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table4` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table5` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
?>
t'en exécute plusieurs à la suite:
https://www.php.net/manual/fr/function.mysql-query.php
exemple :
<?
mysql_query ('INSERT INTO `table1` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table2` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table3` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table4` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
mysql_query ('INSERT INTO `table5` ( `champ1`, `champ2`, `champ3`, `champ4`) VALUES (`val1`, `val2`, `val3`, `val4`, );') or die(mysql_error());
?>
Bonjour a ts,
qd j'essaie d'executer avec wamp la requete insert ci dessous sur Mysql :
mysql_query ('INSERT INTO `article` ( `id`, `source_id`, `type_id`, `collection`,`parution`, `titre`, `acquisition`, `localisation`, `observation`, `prix`, `pertinence`, `projet` ) VALUES NULL, NULL, NULL, `$collection`,`$parution` , `$titre`,`$acquisition`, `$localisation`, `$observation`, `$prix`,`$pertinence`,`$projet`)' ) or die(mysql_error()) ;
ca m'affiche l'erreur suivante :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL, NULL, NULL, `$collection`,`$parution` , `$titre`,`$acquisition`, `$localis' at line 1
auriez vs & idée svp sur la ou ca foire?
mrçi
qd j'essaie d'executer avec wamp la requete insert ci dessous sur Mysql :
mysql_query ('INSERT INTO `article` ( `id`, `source_id`, `type_id`, `collection`,`parution`, `titre`, `acquisition`, `localisation`, `observation`, `prix`, `pertinence`, `projet` ) VALUES NULL, NULL, NULL, `$collection`,`$parution` , `$titre`,`$acquisition`, `$localisation`, `$observation`, `$prix`,`$pertinence`,`$projet`)' ) or die(mysql_error()) ;
ca m'affiche l'erreur suivante :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL, NULL, NULL, `$collection`,`$parution` , `$titre`,`$acquisition`, `$localis' at line 1
auriez vs & idée svp sur la ou ca foire?
mrçi
salut,
si 'id' est 'auto-increment' essaie d'insérer '' plutôt que 'NULL'.
tu utilises des apostrophes pour encadrer ta requête donc les variables ne sont pas interprétées, si les champs sont de type 'Date' ou numériques tu auras une erreur.
les valeurs à insérer doivent être encadrées par des apostrophes simples, pas les mêmes que pour encadrer les noms de champs.
le mieux est d'insérer une ligne avec phpMyAdmin, de récupérer la requête qu'il a généré et de t'en servir pour créer ta propre requête.
si 'id' est 'auto-increment' essaie d'insérer '' plutôt que 'NULL'.
tu utilises des apostrophes pour encadrer ta requête donc les variables ne sont pas interprétées, si les champs sont de type 'Date' ou numériques tu auras une erreur.
les valeurs à insérer doivent être encadrées par des apostrophes simples, pas les mêmes que pour encadrer les noms de champs.
le mieux est d'insérer une ligne avec phpMyAdmin, de récupérer la requête qu'il a généré et de t'en servir pour créer ta propre requête.
mysql_query ("INSERT INTO `article` ( `id`, `source_id`, `type_id`, `collection`,`parution`, `titre`, `acquisition`, `localisation`, `observation`, `prix`, `pertinence`, `projet` ) VALUES '', '', '', '$collection', '$parution' , '$titre', '$acquisition', '$localisation', '$observation', '$prix', '$pertinence', '$projet')" )
bjr a ts
j'ai fait les modif , j'ai tjrs une erreur ke j'arrive pas a comprendre
voici mon code
<html>
<head>
<title>::saisie nouveau article::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include('connect_db.php');
$titre = htmlentities($_POST['titre']);// htmlentities pour éviter l'injection de script
$type=htmlentities($_POST['type']);
$source=htmlentities($_POST['source']);
$localisation = htmlentities($_POST['localisation']);
$projet = htmlentities($_POST['projet']);
$pertinence= htmlentities($_POST['pertinence']);
$acquisition = htmlentities($_POST['acquisition']);
$parution = htmlentities($_POST['parution']);
$resume = htmlentities($_POST['observation']);
$prix= htmlentities($_POST['prix']);
$collection = htmlentities($_POST['collection']);
$sqla="INSERT INTO `basenear`.`type` (
`id` ,nom)
VALUES (
'', '$type')";
//je récupére l'id type qui vient d etre generé
$id_type=mysql_insert_id();
mysql_query($sqla) or die('erreur sql'.$sqla.'<br>'.mysql_error());
$sqlb="INSERT INTO `basenear`.`source` (
`id` ,nom)
VALUES (
'', '$nom')";
//je récupére l'id source qui vient d etre generé
$id_source=mysql_insert_id();
mysql_query($sqlb) or die('erreur sql'.$sqlb.'<br>'.mysql_error());
//la table contenant les 2 clés étrangeres
$sql="INSERT INTO `basenear`.`article` (
`id` ,
`source_id`,
`type_id`,
`collation` ,
`parution` ,
`titre` ,
`acquisition` ,
`localisation` ,
`observation` ,
`prix` ,
`pertinence` ,
`projet`
)
VALUES (
'','$id_source','$id_type', '$collection', '$parution', '$titre', '$acquisition', '$localisation', '$observation', '$prix', '$pertinence', '$projet'
) ";
mysql_query($sql) or die('erreur sql'.$sql.'<br>'.mysql_error());
?>
</body>
</html>
j'ai un fichier forulaire_saisie.php qui contient le formulaire et un autre qui fait la connection
<?php
// Déclaration des paramètres de connexion
$host = 'localhost';
$user = 'root';
$bdd ='basenear';
$passwd = '';
// Connexion au serveur
mysql_connect($host, $user,$passwd) or die("erreur de connexion au serveur");
mysql_select_db($bdd) or die("erreur de connexion a la base de donnees");
?>
merçi pr votre aide
j'ai fait les modif , j'ai tjrs une erreur ke j'arrive pas a comprendre
voici mon code
<html>
<head>
<title>::saisie nouveau article::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include('connect_db.php');
$titre = htmlentities($_POST['titre']);// htmlentities pour éviter l'injection de script
$type=htmlentities($_POST['type']);
$source=htmlentities($_POST['source']);
$localisation = htmlentities($_POST['localisation']);
$projet = htmlentities($_POST['projet']);
$pertinence= htmlentities($_POST['pertinence']);
$acquisition = htmlentities($_POST['acquisition']);
$parution = htmlentities($_POST['parution']);
$resume = htmlentities($_POST['observation']);
$prix= htmlentities($_POST['prix']);
$collection = htmlentities($_POST['collection']);
$sqla="INSERT INTO `basenear`.`type` (
`id` ,nom)
VALUES (
'', '$type')";
//je récupére l'id type qui vient d etre generé
$id_type=mysql_insert_id();
mysql_query($sqla) or die('erreur sql'.$sqla.'<br>'.mysql_error());
$sqlb="INSERT INTO `basenear`.`source` (
`id` ,nom)
VALUES (
'', '$nom')";
//je récupére l'id source qui vient d etre generé
$id_source=mysql_insert_id();
mysql_query($sqlb) or die('erreur sql'.$sqlb.'<br>'.mysql_error());
//la table contenant les 2 clés étrangeres
$sql="INSERT INTO `basenear`.`article` (
`id` ,
`source_id`,
`type_id`,
`collation` ,
`parution` ,
`titre` ,
`acquisition` ,
`localisation` ,
`observation` ,
`prix` ,
`pertinence` ,
`projet`
)
VALUES (
'','$id_source','$id_type', '$collection', '$parution', '$titre', '$acquisition', '$localisation', '$observation', '$prix', '$pertinence', '$projet'
) ";
mysql_query($sql) or die('erreur sql'.$sql.'<br>'.mysql_error());
?>
</body>
</html>
j'ai un fichier forulaire_saisie.php qui contient le formulaire et un autre qui fait la connection
<?php
// Déclaration des paramètres de connexion
$host = 'localhost';
$user = 'root';
$bdd ='basenear';
$passwd = '';
// Connexion au serveur
mysql_connect($host, $user,$passwd) or die("erreur de connexion au serveur");
mysql_select_db($bdd) or die("erreur de connexion a la base de donnees");
?>
merçi pr votre aide
voici mon erreur quand je rempli mes champs du formulaire
erreur sqlINSERT INTO `basenear`.`article` ( `id` , `source_id`, `type_id`, `collation` , `parution` , `titre` , `acquisition` , `localisation` , `observation` , `prix` , `pertinence` , `projet` ) VALUES ( '','19','0', 'volume23 p26', '1930-08-20', 'les miserables', '2007-08-20', 'cl rom', '', '1', '1', 'altr' )
Cannot add or update a child row: a foreign key constraint fails (`basenear/article`, CONSTRAINT `article_ibfk_5` FOREIGN KEY (`type_id`) REFERENCES `type` (`id`) ON DELETE CASCADE)
voici ma table article :
CREATE TABLE `article` (
`id` int(10) NOT NULL auto_increment,
`source_id` int(10) NOT NULL,
`type_id` int(10) NOT NULL,
`collation` varchar(100) character set utf8 NOT NULL,
`parution` date NOT NULL,
`titre` varchar(255) character set utf8 NOT NULL,
`acquisition` date NOT NULL,
`localisation` varchar(100) character set utf8 NOT NULL,
`observation` text character set utf8 NOT NULL,
`prix` float NOT NULL,
`pertinence` int(1) NOT NULL,
`projet` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `source_id` (`source_id`),
KEY `type_id` (`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
table source
CREATE TABLE `source` (
`id` int(10) NOT NULL auto_increment,
`issn` varchar(20) NOT NULL,
`nom` varchar(255) character set utf8 NOT NULL,
`editeur` varchar(255) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
table type
CREATE TABLE `type` (
`id` int(10) NOT NULL auto_increment,
`nom` varchar(100) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
erreur sqlINSERT INTO `basenear`.`article` ( `id` , `source_id`, `type_id`, `collation` , `parution` , `titre` , `acquisition` , `localisation` , `observation` , `prix` , `pertinence` , `projet` ) VALUES ( '','19','0', 'volume23 p26', '1930-08-20', 'les miserables', '2007-08-20', 'cl rom', '', '1', '1', 'altr' )
Cannot add or update a child row: a foreign key constraint fails (`basenear/article`, CONSTRAINT `article_ibfk_5` FOREIGN KEY (`type_id`) REFERENCES `type` (`id`) ON DELETE CASCADE)
voici ma table article :
CREATE TABLE `article` (
`id` int(10) NOT NULL auto_increment,
`source_id` int(10) NOT NULL,
`type_id` int(10) NOT NULL,
`collation` varchar(100) character set utf8 NOT NULL,
`parution` date NOT NULL,
`titre` varchar(255) character set utf8 NOT NULL,
`acquisition` date NOT NULL,
`localisation` varchar(100) character set utf8 NOT NULL,
`observation` text character set utf8 NOT NULL,
`prix` float NOT NULL,
`pertinence` int(1) NOT NULL,
`projet` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `source_id` (`source_id`),
KEY `type_id` (`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
table source
CREATE TABLE `source` (
`id` int(10) NOT NULL auto_increment,
`issn` varchar(20) NOT NULL,
`nom` varchar(255) character set utf8 NOT NULL,
`editeur` varchar(255) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
table type
CREATE TABLE `type` (
`id` int(10) NOT NULL auto_increment,
`nom` varchar(100) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
wi jpense car je récupére les memes 2 id que j'insére ds la table article ou ya les clés étrangéres
<html>
<head>
<title>::saisie nouveau article::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include('connect_db.php');
$titre = htmlentities($_POST['titre']);// htmlentities pour éviter l'injection de script
$type=htmlentities($_POST['type']);
$source=htmlentities($_POST['source']);
$localisation = htmlentities($_POST['localisation']);
$projet = htmlentities($_POST['projet']);
$pertinence= htmlentities($_POST['pertinence']);
$acquisition = htmlentities($_POST['acquisition']);
$parution = htmlentities($_POST['parution']);
$resume = htmlentities($_POST['observation']);
$prix= htmlentities($_POST['prix']);
$collection = htmlentities($_POST['collection']);
$sqla="INSERT INTO `basenear`.`type` (
`id` ,nom)
VALUES (
'', '$type')";
//je récupére l'id type qui vient d etre generé
$id_type=mysql_insert_id(); //ICI
mysql_query($sqla) or die('erreur sql'.$sqla.'<br>'.mysql_error());
$sqlb="INSERT INTO `basenear`.`source` (
`id` ,nom)
VALUES (
'', '$nom')";
//je récupére l'id source qui vient d etre generé // ET ICI
$id_source=mysql_insert_id();
mysql_query($sqlb) or die('erreur sql'.$sqlb.'<br>'.mysql_error());
//la table contenant les 2 clés étrangeres
$sql="INSERT INTO `basenear`.`article` (
`id` ,
`source_id`,
`type_id`,
`collation` ,
`parution` ,
`titre` ,
`acquisition` ,
`localisation` ,
`observation` ,
`prix` ,
`pertinence` ,
`projet`
)
VALUES (
'', '$id_source','$id_type', '$collection', '$parution', '$titre', '$acquisition', '$localisation', '$observation', '$prix', '$pertinence', '$projet'
) ";
mysql_query($sql) or die('erreur sql'.$sql.'<br>'.mysql_error());
?>
</body>
</html>
<html>
<head>
<title>::saisie nouveau article::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include('connect_db.php');
$titre = htmlentities($_POST['titre']);// htmlentities pour éviter l'injection de script
$type=htmlentities($_POST['type']);
$source=htmlentities($_POST['source']);
$localisation = htmlentities($_POST['localisation']);
$projet = htmlentities($_POST['projet']);
$pertinence= htmlentities($_POST['pertinence']);
$acquisition = htmlentities($_POST['acquisition']);
$parution = htmlentities($_POST['parution']);
$resume = htmlentities($_POST['observation']);
$prix= htmlentities($_POST['prix']);
$collection = htmlentities($_POST['collection']);
$sqla="INSERT INTO `basenear`.`type` (
`id` ,nom)
VALUES (
'', '$type')";
//je récupére l'id type qui vient d etre generé
$id_type=mysql_insert_id(); //ICI
mysql_query($sqla) or die('erreur sql'.$sqla.'<br>'.mysql_error());
$sqlb="INSERT INTO `basenear`.`source` (
`id` ,nom)
VALUES (
'', '$nom')";
//je récupére l'id source qui vient d etre generé // ET ICI
$id_source=mysql_insert_id();
mysql_query($sqlb) or die('erreur sql'.$sqlb.'<br>'.mysql_error());
//la table contenant les 2 clés étrangeres
$sql="INSERT INTO `basenear`.`article` (
`id` ,
`source_id`,
`type_id`,
`collation` ,
`parution` ,
`titre` ,
`acquisition` ,
`localisation` ,
`observation` ,
`prix` ,
`pertinence` ,
`projet`
)
VALUES (
'', '$id_source','$id_type', '$collection', '$parution', '$titre', '$acquisition', '$localisation', '$observation', '$prix', '$pertinence', '$projet'
) ";
mysql_query($sql) or die('erreur sql'.$sql.'<br>'.mysql_error());
?>
</body>
</html>
oui évidement y avait qu'à lire...
mais comme je répond au 6 sur le 7... bref !
est-ce que tu as regardé la doc de MySQL ?
morceau choisi :
"Les deux tables doivent être de type InnoDB, dans la table, il doit y avoir un INDEX où les clés étrangères sont listées comme première colonne, dans le même ordre, et dans la table référencée, il doit y avoir un INDEX où les colonnes référencées sont listées comme premières colonnes, dans le même ordre. Les préfixes d'index ne sont pas supportés pour les clés de contrainte."
mais comme je répond au 6 sur le 7... bref !
est-ce que tu as regardé la doc de MySQL ?
morceau choisi :
"Les deux tables doivent être de type InnoDB, dans la table, il doit y avoir un INDEX où les clés étrangères sont listées comme première colonne, dans le même ordre, et dans la table référencée, il doit y avoir un INDEX où les colonnes référencées sont listées comme premières colonnes, dans le même ordre. Les préfixes d'index ne sont pas supportés pour les clés de contrainte."
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
bjr, j'ai testé ma requete sous phpmyadmin sur une table qui contient des clés étrangées
y'a t'il un moyen pour faire abstraction de ces clés
INSERT INTO `basenear`.`article` (
`id` ,
`source_id` ,
`type_id` ,
`collation` ,
`parution` ,
`titre` ,
`acquisition` ,
`localisation` ,
`observation` ,
`prix` ,
`pertinence` ,
`projet`
)
VALUES (
'', '', '', 'vol20 p2', '2007-08-20', 'les misérables', '1982-08-20', 'cl littérature', 'romain de littérature francaise', '2', '2', 'sm/ld '
)
MySQL a répondu:Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`basenear/article`, CONSTRAINT `article_ibfk_4` FOREIGN KEY (`source_id`) REFERENCES `source` (`id`) ON DELETE CASCADE)
mrçi
y'a t'il un moyen pour faire abstraction de ces clés
INSERT INTO `basenear`.`article` (
`id` ,
`source_id` ,
`type_id` ,
`collation` ,
`parution` ,
`titre` ,
`acquisition` ,
`localisation` ,
`observation` ,
`prix` ,
`pertinence` ,
`projet`
)
VALUES (
'', '', '', 'vol20 p2', '2007-08-20', 'les misérables', '1982-08-20', 'cl littérature', 'romain de littérature francaise', '2', '2', 'sm/ld '
)
MySQL a répondu:Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`basenear/article`, CONSTRAINT `article_ibfk_4` FOREIGN KEY (`source_id`) REFERENCES `source` (`id`) ON DELETE CASCADE)
mrçi
je ne sais pas comment gérer ca
mrçi
bon week end