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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| <?php
// PARTIE TRAITEMENTS
// Connexion PDO/MySQL
try {
//connect
$pdo_options [PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=projet', 'root', '', $pdo_options);
}
catch(PDOException $e) {
die('erreur :' .$e->getMessage());
}
// Modification/Insertion/Supprimer
// SI validé
if (isset($_POST['action']) && ($_POST['action'] == 'Ajouter' || $_POST['action'] == 'Modifier' || $_POST['action'] == 'Supprimer')) {
$action = $_POST['action'];
switch ($action) {
case 'Ajouter' :
try {
$stmt = $bdd->prepare('INSERT INTO bibliotheque(codeclient, nomduclient, adrclient)
VALUES (:codeclient, :nomduclient, :adrclient)');
$stmt->bindValue('codeclient', $_POST['codeclient']);
$stmt->bindValue('nomduclient', $_POST['nomduclient']);
$stmt->bindValue('adrclient', $_POST['adrclient']);
$stmt->execute();
$affecte = $stmt->rowCount();
}
catch(PDOException $e) {
exit('ERREUR : ' .$e->getMessage());
}
if ($affecte > 0) {
// Redirection / ré-actualisation
header('Location: gererclient.php');
exit();
}
break;
case 'Modifier' :
// Code pour modifier
if ($affecte > 0) {
// Redirection / ré-actualisation
header('Location: gererclient.php');
exit();
}
break;
case 'Supprimer' :
// Code pour supprimer
if ($affecte > 0) {
// Redirection / ré-actualisation
header('Location: gererclient.php');
exit();
}
break;
}
}
// Récupération de tous les clients
$clients = array();
try {
$stmt_clients = $bdd->query('SELECT codeclient, nomduclient, adrclient
FROM client');
$clients = $stmt_clients->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
$erreur = 'erreur :' .$e->getMessage();
}
// PARTIE INTERFACE / HTML
?>
<html>
<head>
<title>GERER CLIENT</title>
</head>
<body>
<div>
<?php
if (!empty($erreur)) {
echo '<p>'.$erreur.'</p>';
}
foreach ($clients as $client) {
?>
<form action="gererclient.php" method="post">
<fieldset>
<legend>Modifier/Supprimer Client</legend>
<label for="codeclient">Code client</label><input type="text" id="codeclient" name="codeclient" value="<?php echo $client['codeclient']; ?>" />
<br /><label for="nomduclient">nom du client</label><input type="text" id="nomduclient" name="nomduclient"value="<?php echo $client['nomduclient']; ?>" />
<br /><label for="adrclient">Adresse du client</label><input type="text" id="adrclient" name="adrclient"value="<?php echo $client['adrclient']; ?>" />
<input type="submit" name="action" value="Modifier" style="width: 50px" />
<br /><input type="submit" name="action" value="Supprimer" style="width: 50px" />
</fieldset>
</form>
<?php
}
?>
<form action="gererclient.php" method="post">
<fieldset>
<legend>Nouveau Client</legend>
<label for="codeclient">Code client</label><input type="text" id="codeclient" name="codeclient" />
<br /><label for="nomduclient">nom du client</label><input type="text" id="nomduclient" name="nomduclient" />
<br /><label for="adrclient">Adresse du client</label><input type="text" id="adrclient" name="adrclient" />
<br /><input type="submit" name="action" value="Ajouter" style="width: 50px" />
</fieldset>
</form>
</div>
</body>
</html> |
Partager