-
Notifications
You must be signed in to change notification settings - Fork 1
/
addDelete_Product.php
232 lines (194 loc) · 7.84 KB
/
addDelete_Product.php
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
225
226
227
228
229
230
231
232
<?php
//Deshan G G D IT23539372
session_start();
//check if the user is logged in
if(isset($_SESSION['username']))
{
//check if the user is admin
if($_SESSION['user_type']=='Admin')
{
require_once './db_Config/config.php';
}
else if($_SESSION['user_type']=='Manager')
{
header('location: ./manager_DB.php');
}
else
{
header('location: ./index.php');
}
}
else
{
header('location: ./index.php');
}
//Handle the form for creat and update
if($_SERVER['REQUEST_METHOD']==='POST')
{
if(isset($_POST['submitbtn']))
{
$product_name = $_POST['product_name'];
$price = $_POST['price'];
$expire_date = $_POST['expire_date'];
$quantity = $_POST['quantity'];
$description = $_POST['description'];
$img_url = "test.jpg";
if(isset($_FILES['prdct_IMAGE']) && $_FILES['prdct_IMAGE']['error'] === UPLOAD_ERR_OK)
{
$upload_name = $_FILES['prdct_IMAGE']['name'];
// target location
$target_directory = "./Images/product-icons/";
// Move file
move_uploaded_file($_FILES['prdct_IMAGE']['tmp_name'],$target_directory . $upload_name);
// save file name in database
$img_url = $upload_name;
}
$sql = "INSERT INTO Products (product_name, product_description, price, stock_quantity, image_url, expire_date ) VALUES ('$product_name', '$description', '$price', '$quantity', '$img_url', '$expire_date')";
$result = mysqli_query($Connection, $sql);
if($result)
{
echo " <script> alert('Product added!'); </script> ";
header('location: addDelete_Product.php');
}
else
{
echo " <script> alert('Error!'); </script> ";
header('location: addDelete_Product.php');
}
}
}
//Handle the form for delete
if(isset($_POST['deletprdct']))
{
$product_id = $_POST['product_id'];
$sql = "DELETE FROM Products WHERE product_id = '$product_id'";
$result = mysqli_query($Connection, $sql);
if($result)
{
echo " <script> alert('Product deleted!'); </script> ";
header('location: addDelete_Product.php');
}
else
{
echo " <script> alert('Error!'); </script> ";
header('location: addDelete_Product.php');
}
}
//Handle the form for edit
if(isset($_POST['editprdct']))
{
$product_id = $_POST['product_id'];
$qty = $_POST['qtyedit'];
$price = $_POST['Priceedt'];
$sql = "UPDATE Products SET stock_quantity = '$qty', price = '$price' WHERE product_id = '$product_id'";
$result = mysqli_query($Connection, $sql);
if($result)
{
echo " <script> alert('Product updated!'); </script> ";
header('location: addDelete_Product.php');
}
else
{
echo " <script> alert('Error!'); </script> ";
header('location: addDelete_Product.php');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Products</title>
<link rel="stylesheet" href="./CSS/index.css">
<link rel="stylesheet" href="./CSS/addDelete_Product.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="icon" type="image/png" sizes="32x32" href="./Images/Pharmacy X Icon.png">
<link rel="icon" type="image/png" sizes="16x16" href="./Images/Pharmacy X Icon.png">
<link rel="stylesheet" href="../CSS/addDelete_Product.css">
<link rel="stylesheet" href="./CSS/index.css">
</head>
<body>
<?php include ("./header.php"); ?>
<div class="product-form">
<h2>Product Add</h2>
<div class="add-product-form">
<div class="add-product-form-container">
<form action="addDelete_Product.php" method ="POST" enctype="multipart/form-data">
<div class="add-porduct-row">
<input type="text" class="add-products-inputs product-name" placeholder="Product Name" name="product_name" onkeydown="checkProductDetails();">
<input type="text" class="add-products-inputs product-name-input product-price" placeholder="Price" name="price" onkeydown="checkProductDetails();">
</div>
<div class="add-product-row">
<input type="date" class="add-products-inputs" placeholder="Expire Date" name="expire_date" >
<input type="text" class="add-products-inputs" placeholder="Quantity" name="quantity" >
</div>
<div class="add-product row">
<input type="file" class="add-product-inputs choose-file-input" name="prdct_IMAGE">
</div>
<div class="add-product-row description_box">
<textarea name="description" placeholder="Description" cols="55" rows="4" ></textarea>
</div>
<div class="error-content"></div>
<div class="submit-button-container">
<button class="submit-button" name="submitbtn" >Submit</button>
</div>
</form>
</div>
</div>
</div>
<div class="product-container">
<div class="product-heading">
<h2 class="product-list-heading">Products</h2>
</div>
<div class="products-list-container">
<table>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Action</th>
<th>Action</th>
</tr>
<?php
$sql = "SELECT * FROM Products;";
$result = mysqli_query($Connection, $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$pID = $row['product_id'];
$pName = $row['product_name'];
$qty = $row['stock_quantity'];
$P_price = $row['price'];
echo "<tr>
<td>$pID</td>
<td>$pName</td>
<form action='addDelete_Product.php' method='POST'>
<td><input type='number' value='$qty' min='0' name='qtyedit'></td>
<td><input type='text' value='$P_price' name='Priceedt'></td>
<input type='hidden' name='product_id' value='$pID'>
<td><button type='submit' name='editprdct' class='edit-product-button'>Edit</button></td>
<td><button type='submit' name='deletprdct' class='delete-product-button'>Delete</button></td>
</form>
</tr>";
}
}
else
{
echo "<tr>";
echo "<td colspan='6'>No products found</td>";
echo "</tr>";
}
$Connection->close();
?>
</table>
</div>
</div>
<?php include ("./footer.php"); ?>
<script src="./JS/addDelete_Product.js"></script>
</body>
</html>