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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Administration</title>
</head>
<body>
<?php
include"connexion.php";
$bdd = Connect_db();
if(isset($_POST) AND count($_POST)>0)
{
if (isset($_POST['nom']) AND isset($_POST['prix']) AND isset($_POST['ingredients'])) //Si le formulaire a été envoyé
{
if ($_POST['nom'] == NULL) // Le nom n'est pas renseigné
{
echo 'Veuilez entrer un nom pour votre pizza';
}
else if ($_POST['prix'] == NULL) // Le prix n'est pas renseigné
{
echo 'Veuilez entrer un prix pour votre pizza';
}
else //Tous les champs sont remplis
{
$id_nom=htmlspecialchars($_POST['nom']);
$id_nom_maj = strtoupper(htmlspecialchars($_POST['nom']));
$id_nom_maj=strtolower(str_replace(" ", "", $id_nom_maj));
$id_prix = htmlspecialchars($_POST['prix']);
$req_nom_pizza = $bdd->prepare('SELECT NAME_PIZZA,ID_PIZZA FROM pizzas WHERE upper(NAME_PIZZA) = ?');
$req_nom_pizza->execute(array($id_nom_maj));
$data=$req_nom_pizza->fetch();
$id_pizza = $data['ID_PIZZA'];
$id_nom_bdd= $data['NAME_PIZZA'];
$req_nom_pizza->closeCursor();
if($data!=NULL AND count($data)>0) // Si la pizza existe, on la met à jour
{
/* REQUETE A METTRE A JOUR AVEC LA NOUVELLE BDD !! */
$req_prix = $bdd->exec("UPDATE pizzas SET PRICE_PIZZA=$id_prix WHERE NAME_PIZZA = \"".$id_nom_bdd."\"");
/* On va chercher la liste des ingredients */
$req_id_ingr = $bdd->prepare('SELECT ID_INGREDIENT FROM ingredients WHERE LABEL_INGREDIENT = ?');
$req_suppr = $bdd->exec("DELETE FROM pizzas_ingredients WHERE ID_PIZZA = $id_pizza"); // On supprime les anciens ingrédients pour pouvoir ajouter les nouveaux !
$req_insertion_asso= $bdd -> prepare("INSERT INTO pizzas_ingredients(ID_PIZZA, ID_INGREDIENT) VALUES (?, ?)");
foreach ($_POST['ingredients'] as $ingr)
{
$req_id_ingr->execute(array($ingr)); // Pour chaque ingrédient spécifié, on récupère son id
$data_id_ingr = $req_id_ingr->fetch();
// On ajoute l'association pizza/ingrédient !
$req_insertion_asso -> execute(array($id_pizza,$data_id_ingr['ID_INGREDIENT']));
}
$req_id_ingr->closeCursor();
echo '<section class="message">
Cette pizza existe déjà et a été modifiée.
</section>';
}
else // Si la pizza n'existe pas, on l'ajoute
{
$req_insertion = $bdd->exec("INSERT INTO pizzas (NAME_PIZZA, PRICE_PIZZA) VALUES(\"".$id_nom."\", $id_prix)");
/* On va chercher la liste des ingredients */
$req_id_ingr = $bdd->prepare('SELECT ID_INGREDIENT FROM ingredients WHERE LABEL_INGREDIENT = ?');
$req_id_pizza = $bdd->prepare("SELECT ID_PIZZA FROM pizzas WHERE NAME_PIZZA = ?");
$req_id_pizza->execute(array($id_nom));
$data_id_pizza = $req_id_pizza->fetch(); // $data_id_pizza contient l'id de la pizza ajoutée
$req_insertion_asso= $bdd -> prepare("INSERT INTO pizzas_ingredients(ID_PIZZA, ID_INGREDIENT) VALUES (?, ?)");
foreach ($_POST['ingredients'] as $ingr)
{
$req_id_ingr->execute(array($ingr)); // Pour chaque ingrédient spécifié, on récupère son id
$data_id_ingr = $req_id_ingr->fetch(); // $data_id_ingr contient l'id des ingrédients
// On ajoute l'association pizza/ingrédient !
$req_insertion_asso -> execute(array($data_id_pizza['ID_PIZZA'],$data_id_ingr['ID_INGREDIENT']));
}
$req_id_ingr->closeCursor();
echo '<section class="message">
La pizza a été ajoutée.
</section>';
}
if (isset($_FILES['fichier']) AND $_FILES['fichier']['error'] == 0 AND $_FILES['fichier']['size'] <= 2097152) // 2MO
{
$infosfichier = pathinfo($_FILES['fichier']['name']);
$ext_upload = $infosfichier['extension'];
$ext_autorisees = array('jpg', 'jpeg', 'png');
if (in_array($ext_upload, $ext_autorisees))
{
$nom_fichier=strtolower(str_replace(" ", "", $id_nom));
$query = $bdd->prepare('SELECT IMAGE_NAME FROM pizzas WHERE NAME_PIZZA=? AND IMAGE_NAME IS NOT NULL');
$query->execute(array($id_nom));
$data=$query->fetch();
if ($data['IMAGE_NAME']!=NULL)
{
echo "ancien fichier:".$data['IMAGE_NAME'];
unlink("./Images/Pizzas/".$data['IMAGE_NAME']);
}
$query->closeCursor();
move_uploaded_file($_FILES['fichier']['tmp_name'],'./images/pizzas/' . $nom_fichier.'.'.$ext_upload);
$query = $bdd->prepare('UPDATE pizzas SET IMAGE_NAME= ? WHERE NAME_PIZZA=?');
$query->execute(array($nom_fichier.".".$ext_upload,$id_nom));
}
}
}
}
}
else
{ // Si on a pas rempli le formulaire, on l'affiche
?>
<section id="ajout_pizza">
<h1>Ajouter une nouvelle pizza au menu</h1>
<form action="#" method="post" enctype="multipart/form-data">
<div><label for="nom">Nom</label><input type="text" id="nom" name="nom" placeholder="Le nom de notre futur best-seller"/></div>
<div id="ingr">Ingrédients : <br />
<?php
/* On va chercher la liste des ingredients */
$req = $bdd->prepare('SELECT LABEL_INGREDIENT FROM ingredients');
$req->execute();
while ($data = $req->fetch())
{
$label_ingr = $data['LABEL_INGREDIENT'];
echo '<input type="checkbox" name="ingredients[]" value="'.$label_ingr.'" id="'.$label_ingr.'" /><label for="'.$label_ingr.'">'.$label_ingr.'</label><br />';
}
$req->closeCursor();
?>
<div><label for="prix">Prix</label><input type="text" id="prix" name="prix" placeholder="Le prix unitaire en euros"/></div>
<div><label for="fichier">Image (optionnelle, d'extension jpg ou png et de taille < 2Mo)</label><input type="file" name="fichier" id="fichier" /></div>
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
<div>
<button type="submit">Enregistrer</button>
</div>
</form>
</section>
<?php
} //fin else
?>
</body>
</html> |