-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_product.php
More file actions
161 lines (153 loc) · 6.6 KB
/
add_product.php
File metadata and controls
161 lines (153 loc) · 6.6 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
<?php
// Include the database configuration
session_start();
include('config.php');
if (!isset($_SESSION["user_id"])) {
header("Location: login.php");
exit();
}
// Check if the form is submitted
if (isset($_POST['Add'])) {
// Retrieve form inputs
$NAME = $_POST['name'];
$PRICE = $_POST['price'];
$DESCRIPTION = $_POST['description'];
$STOCK = $_POST['stock'];
// Handle file upload
$IMAGE = $_FILES['image'];
$image_location = $_FILES['image']['tmp_name'];
$image_name = $_FILES['image']['name'];
$image_up = "assets/images/" . $image_name;
// SQL query to insert product into the database
$add = "INSERT INTO products (name, description, price, stock, image)
VALUES ('$NAME', '$DESCRIPTION', '$PRICE', '$STOCK', '$image_up')";
if (mysqli_query($conn, $add)) {
// Move the uploaded file to the `images` directory
if (move_uploaded_file($image_location, $image_up)) {
echo "<script>alert('Product added successfully!');</script>";
} else {
echo "<script>alert('Failed to upload the image.');</script>";
}
} else {
echo "<script>alert('Error adding product: " . mysqli_error($conn) . "');</script>";
}
header("Location:products.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Product - Honey E-Commerce</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Optional: Custom CSS for consistent design -->
<style>
body {
background-color: #f8f9fa;
}
.navbar-nav .nav-link {
color: #343a40 !important;
}
.card-header {
background-color: #ffc107;
color: #343a40;
}
</style>
</head>
<body>
<!-- Navigation Bar (similar to products.php) -->
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="index.php">Honey E-Commerce</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<!-- You can add menu items as needed -->
<li class="nav-item">
<a class="nav-link" href="products.php">Back to products</a>
</li>
<!-- Optionally, links to Login/Logout based on session -->
</ul>
</div>
</div>
</nav>
<!-- Main Container -->
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<!-- Card Container to match the style from products.php -->
<div class="card shadow">
<div class="card-header text-center">
<h3 class="mb-0">Add New Product</h3>
</div>
<div class="card-body">
<form action="add_product.php" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="name" class="form-label">Product Name</label>
<input
type="text"
class="form-control"
id="name"
name="name"
placeholder="Enter product name"
required>
</div>
<div class="mb-3">
<label for="price" class="form-label">Price ($)</label>
<input
type="number"
class="form-control"
id="price"
name="price"
placeholder="Enter product price"
step="0.01"
required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea
class="form-control"
id="description"
name="description"
placeholder="Enter product description"
rows="4"
required></textarea>
</div>
<div class="mb-3">
<label for="stock" class="form-label">Stock</label>
<input
type="number"
class="form-control"
id="stock"
name="stock"
placeholder="Enter stock quantity"
required>
</div>
<div class="mb-3">
<label for="image" class="form-label">Product Image</label>
<input
type="file"
class="form-control"
id="image"
name="image"
accept="image/*"
required>
</div>
<div class="text-center">
<button type="submit" name="Add" class="btn btn-primary">Add Product</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap JS Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>