1 pièce(s) jointe(s)
Insérer des données dans 2 (deux) tables simultanément ayant des clés étrangères?
Bonjour ,
Je suis actuellement bloqué sur un problème dans mon projet de fin de cycle BTS Informatique, le problème est que je veux insérer des données dans une table SIMULTANÉMENT contenant une clé étrangère à partir d'un seul formulaire avec PDO . l'id de la table migre dans la table bureau
Code:
1 2 3 4 5 6 7
| CREATE TABLE IF NOT EXISTS `bureau` (
`idbureau` int(11) NOT NULL AUTO_INCREMENT,
`designation` varchar(225) DEFAULT NULL,
`service_idservice` int(11) NOT NULL,
PRIMARY KEY (`idbureau`,`service_idservice`),
KEY `fk_bureau_service_idx` (`service_idservice`)
) ENGINE=InnoDB AUTO_INCREMENT=20058 DEFAULT CHARSET=utf8; |
Code:
1 2 3 4 5
| CREATE TABLE IF NOT EXISTS `service` (
`idservice` int(11) NOT NULL AUTO_INCREMENT,
`designation` varchar(225) DEFAULT NULL,
PRIMARY KEY (`idservice`)
) ENGINE=InnoDB AUTO_INCREMENT=10043 DEFAULT CHARSET=utf8; |
merci de m'aide :zoubi:
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <?php
// include database connection file
require_once'dbconfig.php';
if(isset($_POST['insert']))
{
// Posted Values
$bureau=$_POST['bureau'];
$service=$_POST['service'];
// Query for Insertion
$sql="INSERT INTO bureau (designation ,service_idservice ) VALUES(:b,:s)";
//Prepare Query for Execution
$query = $DB_con->prepare($sql);
// Bind the parameters
$query->bindParam(':b',$bureau,PDO::PARAM_STR);
$query->bindParam(':s',$service,PDO::PARAM_STR);
// Query Execution
$query->execute();
// Check that the insertion really worked. If the last inserted id is greater than zero, the insertion worked.
$lastInsertId = $DB_con->lastInsertId();
if($lastInsertId)
{
// Message for successfull insertion
echo "<script>alert('Enregistrement inséré avec succès');</script>";
echo "<script>window.location.href='burserv.php'</script>";
}
else
{
// Message for unsuccessfull insertion
echo "<script>alert('Quelque chose a mal tourné. Veuillez réessayer');</script>";
echo "<script>window.location.href='buserv.php'</script>";
}
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>PHP CURD Operation using PDO Extension </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h3>Enregistrement de Bureau&Service |</h3>
<hr />
</div>
</div>
<form name="insertrecord" method="post">
<div class="row">
<div class="col-md-4"><b>Bureau</b>
<input type="text" name="bureau" class="form-control" required>
</div>
<div class="col-md-4"><b>Service</b>
<input type="text" name="service" class="form-control" required>
</div>
</div>
<div class="row" style="margin-top:1%">
<div class="col-md-8">
<input type="submit" name="insert" value="Submit">
</div>
</div>
</form>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- textaddneww -->
<ins class="adsbygoogle"
style="display:inline-block;width:728px;height:15px"
data-ad-client="ca-pub-8906663933481361"
data-ad-slot="3318815534"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</div>
</body>
</html> |