-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_products.php
121 lines (92 loc) · 4.05 KB
/
admin_products.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
<?php
@include 'config.php';
session_start();
$admin_id = $_SESSION['admin_id'];
if(!isset($admin_id)){
header('location:home.php');
};
if(isset($_POST['add_product'])){
$name = mysqli_real_escape_string($conn, $_POST['name']);
$price = mysqli_real_escape_string($conn, $_POST['price']);
$details = mysqli_real_escape_string($conn, $_POST['details']);
$image = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
$image_folter = 'uploaded_img/'.$image;
$select_product_name = mysqli_query($conn, "SELECT name FROM `products` WHERE name = '$name'") or die('query failed');
if(mysqli_num_rows($select_product_name) > 0){
$message[] = 'product name already exist!';
}else{
$insert_product = mysqli_query($conn, "INSERT INTO `products`(name, details, price, image) VALUES('$name', '$details', '$price', '$image')") or die('query failed');
if($insert_product){
if($image_size > 2000000){
$message[] = 'Image size is too large!';
}else{
move_uploaded_file($image_tmp_name, $image_folter);
$message[] = 'Product added successfully!';
}
}
}
}
if(isset($_GET['delete'])){
$delete_id = $_GET['delete'];
$select_delete_image = mysqli_query($conn, "SELECT image FROM `products` WHERE id = '$delete_id'") or die('query failed');
$fetch_delete_image = mysqli_fetch_assoc($select_delete_image);
unlink('uploaded_img/'.$fetch_delete_image['image']);
mysqli_query($conn, "DELETE FROM `products` WHERE id = '$delete_id'") or die('query failed');
mysqli_query($conn, "DELETE FROM `wishlist` WHERE pid = '$delete_id'") or die('query failed');
mysqli_query($conn, "DELETE FROM `cart` WHERE pid = '$delete_id'") or die('query failed');
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>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="css/admin_style.css">
</head>
<body>
<?php @include 'admin_header.php'; ?>
<br><br><h1 class="title">New Products</h1>
<section class="add-products">
<form action="" method="POST" name="myForm" enctype="multipart/form-data">
<h3>add new product</h3>
<input type="text" class="box" required placeholder="Enter product name" name="name">
<input type="number" min="0" class="box" required placeholder="Enter product price" name="price">
<textarea name="details" class="box" required placeholder="Enter product details" cols="30" rows="10"></textarea>
<input type="file" accept="image/jpg, image/jpeg, image/png" required class="box" name="image">
<input type="submit" value="add product" name="add_product" class="btn">
</form>
</section>
<br><br>
<h1 class="title">Existing Products</h1>
<section class="show-products">
<div class="box-container">
<?php
$select_products = mysqli_query($conn, "SELECT * FROM `products`") or die('query failed');
if(mysqli_num_rows($select_products) > 0){
while($fetch_products = mysqli_fetch_assoc($select_products)){
?>
<div class="box">
<img class="image" src="uploaded_img/<?php echo $fetch_products['image']; ?>" alt="">
<div class="name"><?php echo $fetch_products['name']; ?></div>
<div class="price">Rs. <?php echo $fetch_products['price']; ?>/-</div>
<a href="admin_update_product.php?update=<?php echo $fetch_products['id']; ?>" class="option-btn">update</a>
<a href="admin_products.php?delete=<?php echo $fetch_products['id']; ?>" class="delete-btn" onclick="return confirm('delete this product?');">delete</a>
</div>
<?php
}
}else{
echo '<p class="empty">No products added yet!</p>';
}
?>
</div>
</section>
<script src="js/admin_script.js">
</script>
</body>
</html>