-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_products.php
More file actions
167 lines (121 loc) · 5 KB
/
admin_products.php
File metadata and controls
167 lines (121 loc) · 5 KB
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
<?php
@include 'config.php';
session_start();
$admin_id = $_SESSION['admin_id'];
if(!isset($admin_id)){
header('location:login.php');
};
if(isset($_POST['add_product'])){
$name = $_POST['name'];
$name = filter_var($name, FILTER_SANITIZE_STRING);
$price = $_POST['price'];
$price = filter_var($price, FILTER_SANITIZE_STRING);
$category = $_POST['category'];
$category = filter_var($category, FILTER_SANITIZE_STRING);
$details = $_POST['details'];
$details = filter_var($details, FILTER_SANITIZE_STRING);
$image = $_FILES['image']['name'];
$image = filter_var($image, FILTER_SANITIZE_STRING);
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
$image_folder = 'uploaded_img/'.$image;
$select_products = $conn->prepare("SELECT * FROM `products` WHERE name = ?");
$select_products->execute([$name]);
if($select_products->rowCount() > 0){
$message[] = 'product name already exist!';
}else{
$insert_products = $conn->prepare("INSERT INTO `products`(name, category, details, price, image) VALUES(?,?,?,?,?)");
$insert_products->execute([$name, $category, $details, $price, $image]);
if($insert_products){
if($image_size > 2000000){
$message[] = 'image size is too large!';
}else{
move_uploaded_file($image_tmp_name, $image_folder);
$message[] = 'new product added!';
}
}
}
};
if(isset($_GET['delete'])){
$delete_id = $_GET['delete'];
$select_delete_image = $conn->prepare("SELECT image FROM `products` WHERE id = ?");
$select_delete_image->execute([$delete_id]);
$fetch_delete_image = $select_delete_image->fetch(PDO::FETCH_ASSOC);
unlink('uploaded_img/'.$fetch_delete_image['image']);
$delete_products = $conn->prepare("DELETE FROM `products` WHERE id = ?");
$delete_products->execute([$delete_id]);
$delete_wishlist = $conn->prepare("DELETE FROM `wishlist` WHERE pid = ?");
$delete_wishlist->execute([$delete_id]);
$delete_cart = $conn->prepare("DELETE FROM `cart` WHERE pid = ?");
$delete_cart->execute([$delete_id]);
header('location:admin_products.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>products</title>
<!-- font awesome cdn link -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
<!-- custom css file link -->
<link rel="stylesheet" href="css/admin_style.css">
</head>
<body>
<?php include 'admin_header.php'; ?>
<section class="add-products">
<h1 class="title">add new product</h1>
<form action="" method="POST" enctype="multipart/form-data">
<div class="flex">
<div class="inputBox">
<input type="text" name="name" class="box" required placeholder="enter product name">
<select name="category" class="box" required>
<option value="" selected disabled>select category</option>
<option value="Devotional Books">Devotional Books</option>
<option value="Jewellery">Jewellery</option>
<option value="Idols statue">Idols statue</option>
<option value="Candles">Candles</option>
</select>
</div>
<div class="inputBox">
<input type="number" min="0" name="price" class="box" required placeholder="enter product price">
<input type="file" name="image" required class="box" accept="image/jpg, image/jpeg, image/png">
</div>
</div>
<textarea name="details" class="box" required placeholder="enter product details" cols="30" rows="10"></textarea>
<input type="submit" class="btn" value="add product" name="add_product">
</form>
</section>
<section class="show-products">
<h1 class="title">products added</h1>
<div class="box-container">
<?php
$show_products = $conn->prepare("SELECT * FROM `products`");
$show_products->execute();
if($show_products->rowCount() > 0){
while($fetch_products = $show_products->fetch(PDO::FETCH_ASSOC)){
?>
<div class="box">
<div class="price">Rs.<?= $fetch_products['price']; ?>/-</div>
<img src="uploaded_img/<?= $fetch_products['image']; ?>" alt="">
<div class="name"><?= $fetch_products['name']; ?></div>
<div class="cat"><?= $fetch_products['category']; ?></div>
<div class="details"><?= $fetch_products['details']; ?></div>
<div class="flex-btn">
<a href="admin_update_product.php?update=<?= $fetch_products['id']; ?>" class="option-btn">update</a>
<a href="admin_products.php?delete=<?= $fetch_products['id']; ?>" class="delete-btn" onclick="return confirm('delete this product?');">delete</a>
</div>
</div>
<?php
}
}else{
echo '<p class="empty">now products added yet!</p>';
}
?>
</div>
</section>
<script src="js/script.js"></script>
</body>
</html>