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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
<?php
session_start();
?>
<link href="../styles/main.css" type="text/css" rel="stylesheet"/>
<h1>
Bienvenue,
<?php echo $_SESSION['username']; ?>
</h1>
<br/>
<a href="?action=add">Ajouter un produit</a>
<a href="?action=modifyanddelete">Modifier / Supprimer un produit</a><br/><br/>
<?php
try
{
$db = new PDO('mysql:host=localhost;dbname=site-e-commerce', 'root', '');
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e){
echo'Une erreur est survenue';
die();
}
if(isset($_SESSION['username'])){
if(isset($_GET['action'])){
if($_GET['action']=='add'){
if(isset($_POST['submit'])){
$title=$_POST['title'];
$description=$_POST['description'];
$price=$_POST['price'];
$img = $_FILES['img']['name'];
$img_tmp = $_FILES['img']['tmp_name'];
if(!empty($img_tmp)){
$image = explode('.', $img);
$image_ext = end($image);
if(in_array(strtolower($image_ext),array('png','jpg','jpeg'))===false){
echo'Veuillez insérer une image respectant les conditions requises.';
}else{
$image_size = getimagesize($img_tmp);
if($image_size['mime']=='image/jpeg'){
$image_src = imagecreatefromjpeg($img_tmp);
}else if($image_size['mime']=='image/png'){
$image_src = imagecreatefrompng($img_tmp);
}else{
$image_src = false;
echo'Veuillez insérer une image valide.';
}
if($image_src!==false){
$image_width=300;
if($image_size[0]==$image_width){
$image_finale = $image_src;
}else{
$new_width[0]=$image_width;
$new_height[1]= 300;
$image_finale = imagecreatetruecolor($new_width[0],$new_height[1]);
imagecopyresampled($image_finale,$image_src,0,0,0,0,$new_width[0],$new_height[1],$image_size[0],$image_size[1]);
}
imagejpeg($image_finale, 'imgs/' .$title. '.jpg');
}
}
}else{
echo'Veuillez insérer une image';
}
if($title&&$description&&$price){
$db = new PDO('mysql:host=localhost;dbname=site-e-commerce', 'root', '');
$insert = $db->prepare("INSERT INTO products VALUES('', $title', '$description', '$price')");
$insert->execute();
}else{
echo'veuillez remplir tous les champs';
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<h3>Titre du produit:</h3><input type="text" name="title"/>
<h3>Description du produit:</h3><textarea name="description"></textarea>
<h3>Prix:</h3><input type="text" name="price"/><br/><br/>
<h3>Image:</h3><input type="file" name="img"/><br/><br/>
<input type="submit" name="submit"/>
</form>
<?php
}else if($_GET['action']=='modifyanddelete'){
$db = new PDO('mysql:host=localhost;dbname=site-e-commerce', 'root', '');
$select = $db->prepare("SELECT * FROM products");
$select->execute();
while($s=$select->fetch(PDO::FETCH_OBJ)){
echo $s->title;
?>
<a href="?action=modify&id=<?php echo $s->id; ?>">Modifier</a>
<a href="?action=delete&id=<?php echo $s->id; ?>">Supprimer</a><br/><br/>
<?php
}
}else if($_GET['action']=='modify'){
$id=$_GET['id'];
$db = new PDO('mysql:host=localhost;dbname=site-e-commerce', 'root', '');
$select = $db->prepare("SELECT * FROM products WHERE id=$id");
$select->execute();
$data = $select->fetch(PDO::FETCH_OBJ);
?>
<form action="" method="post">
<h3>Titre du produit:</h3><input value="<?php echo $data->title; ?>" type="text" name="title"/>
<h3>Description du produit:</h3><textarea name="description"><?php echo $data->description; ?></textarea>
<h3>Prix:</h3><input value="<?php echo $data->price; ?>" type="text" name="price"/><br/><br/>
<input type="submit" name="submit" value="Modifier"/>
</form>
<?php
if(isset($_POST['submit'])){
$title=$_POST['title'];
$description=$_POST['description'];
$price=$_POST['price'];
$db = new PDO('mysql:host=localhost;dbname=site-e-commerce', 'root', '');
$update = $db->prepare("UPDATE products SET title='$title', description='$description', price='$price' WHERE id=$id");
$update->execute();
header('Location: admin.php?action=modifyanddelete');
}
}else if($_GET['action']=='delete'){
$db = new PDO('mysql:host=localhost;dbname=site-e-commerce', 'root', '');
$id=$_GET['id'];
$delete = $db->prepare("DELETE FROM products WHERE id=$id");
$delete->execute();
}else{
die('Une erreur s\est produite.');
}
}
}else{
header('Location: ../index.php');
}
?> |
Partager