-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd_product.php
349 lines (282 loc) · 13.4 KB
/
add_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
session_start();
include 'dbconfig.php';
include "redisconnect.php";
$me = $_COOKIE['PHPSESSID'];
$logged = $redis->hgetall("user:$me");
// Select the last inserted product ID from the product table
$sql = "SELECT MAX(product_id) as last_product_id FROM product";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while ($row = $result->fetch_assoc()) {
$_SESSION['lastProductId'] = $row["last_product_id"];
// echo "Last inserted product ID: " . $lastProductId;
// You can store $lastProductId in a PHP variable for further use
}
} else {
echo "No products found";
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Item</title>
<link rel="stylesheet" href="add.css">
<script src="title.js"></script>
<script src="https://kit.fontawesome.com/f7e75704ad.js" crossorigin="anonymous"></script>
<!-- <script type="module" src="upload_image.js"></script> -->
</head>
<body>
<header>
<h1>
Add item to Inventory
</h1>
</header>
<div class="container">
<h2>Add New Item</h2>
<form id="productForm" action="add_product.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="description">Description:</label>
<textarea id="description" name="description" rows="3" required></textarea>
<label for="price">Price:</label>
<input type="number" id="price" name="price" step="0.01" required>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" required>
<label for="img_url">Add product image:</label>
<input type="text" id="img_url" name="img_url" style="display: none;">
<!-- Buttons for image upload -->
<input type="file" id="imageInput" accept="image/*" style="display: none;">
<div class="spacebtn">
<button type="button" onclick="openFilePicker()">Choose Image</button>
<button type="button" onclick="openCamera()">Capture Image</button>
</div>
<br>
<br>
<div id="imgdiv">
<img id="img" src="" alt="">
</div>
<br>
<button type="button" id="upld">upload selected image</button>
<br>
<br>
<div class="spacebtn">
<button type="button" onclick="toInventory()"><i class="fa-solid fa-angles-left" style="color: #ffffff;"></i></i></button>
<input type="submit" value="Add Product">
</div>
</form>
<?php
include 'dbconfig.php';
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if all required fields are present
if (!empty($_POST['img_url'])) {
if (isset($_POST['name']) && isset($_POST['description']) && isset($_POST['price']) && isset($_POST['quantity'])) {
// Sanitize inputs to prevent SQL injection
$name = htmlspecialchars($_POST['name']);
$description = htmlspecialchars($_POST['description']);
$price = floatval($_POST['price']); // Convert to float for price
$quantity = intval($_POST['quantity']); // Convert to integer for quantity
$img_url = htmlspecialchars($_POST['img_url']);
// Additional sanitization and validation can be added here
// Example user and merchant values (adjust as needed)
if (isset($logged['merchantid'])) {
$user = $logged['userid'];
$merchant = $logged['merchantid'];
// Prepare and bind parameters for the SQL statement
$sql = "INSERT INTO product (name, description, price, quantity, img_url, user_id, merchant_id) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssdissi", $name, $description, $price, $quantity, $img_url, $user, $merchant);
// Execute the SQL statement
if ($stmt->execute()) {
echo "Item added successfully.";
// You can redirect the user to another page if needed
// header("Location: inventory.php");
// exit();
} else {
echo "Error adding item: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
} else {
echo 'mid empty';
}
$conn->close();
} else {
echo "All fields are required.";
}
} else {
echo "<script>";
echo "alert('Ensure you have selected and uploaded an image');";
echo "</script>";
exit;
}
}
?>
</div>
<footer>
<p style="font-size: 10px;">
© 2024 posperity,all rights reserved</p>
</footer>
<script>
function toInventory() {
// Redirect to another page (replace 'page-url' with the actual URL)
window.location.href = 'inventory.php';
}
// Function to open file picker for storage selection
function openFilePicker() {
document.getElementById('imageInput').click();
}
// Function to open camera for picture capture
function openCamera() {
const constraints = {
video: {
facingMode: 'environment'
} // Use the back camera
};
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
const videoElement = document.createElement('video');
videoElement.srcObject = stream;
videoElement.play();
// Append the video element to the document for preview
document.getElementById("imgdiv").appendChild(videoElement);
// Create a button for capturing the image
const captureButton = document.createElement('button');
captureButton.id = 'upld';
captureButton.textContent = 'Capture Image';
captureButton.onclick = () => {
// Create a canvas element to capture the image
const canvas = document.createElement('canvas');
canvas.width = videoElement.videoWidth;
canvas.height = videoElement.videoHeight;
const context = canvas.getContext('2d');
// Draw the video frame onto the canvas
context.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
// Convert the canvas content to a data URL (base64-encoded image)
const dataURL = canvas.toDataURL('image/jpeg');
// Save the data URL to localStorage or sessionStorage
localStorage.setItem('capturedImage', dataURL);
// Retrieve the captured image data from localStorage
const capturedImageDataURL = localStorage.getItem('capturedImage');
console.log('Image saved to localStorage');
// Display the captured image in an <img> element
if (capturedImageDataURL) {
const imageElement = document.getElementById("img");
imageElement.src = capturedImageDataURL;
console.log('upload shown.');
} else {
console.error('No captured image data found.');
}
// Stop the video stream and close the camera
stream.getTracks().forEach(track => track.stop());
videoElement.remove();
captureButton.remove();
// Navigate to a new page or perform other actions
// Replace with your desired page
};
// Append the capture button to the document
document.getElementById("imgdiv").appendChild(captureButton);
})
.catch(error => {
console.error('Error accessing camera:', error);
});
}
document.addEventListener('DOMContentLoaded', () => {
const imagePicker = document.getElementById('imageInput');
// Event listener for when an image is selected using the file picker
imagePicker.addEventListener('change', handleImageSelection);
});
function handleImageSelection(event) {
const selectedFile = event.target.files[0]; // Get the selected file (image)
if (selectedFile) {
// Create a FileReader object to read the file as a data URL
const reader = new FileReader();
// Event listener for when the FileReader has successfully read the file
reader.onload = function(event) {
const imageDataURL = event.target.result; // Get the data URL of the selected image
localStorage.setItem('capturedImage', imageDataURL); // Save the image data URL to localStorage
console.log('Image saved to localStorage');
const capturedImageDataURL = localStorage.getItem('capturedImage');
const imageElement = document.getElementById("img");
imageElement.src = capturedImageDataURL;
};
// Read the selected file as a data URL
reader.readAsDataURL(selectedFile);
}
}
</script>
</body>
<!-- -->
<script type="module">
import {
initializeApp
} from "https://www.gstatic.com/firebasejs/10.8.1/firebase-app.js";
import {
getStorage,
ref,
uploadString
} from "https://www.gstatic.com/firebasejs/10.8.1/firebase-storage.js";
import {
getDownloadURL
} from "https://www.gstatic.com/firebasejs/10.8.1/firebase-storage.js";
const firebaseConfig = {
apiKey: "AIzaSyDdsL0Sf4IVmqlX05cx5gZ1wqrqWRC4j2c",
authDomain: "posperity.firebaseapp.com",
projectId: "posperity",
storageBucket: "posperity.appspot.com",
messagingSenderId: "210695590267",
appId: "1:210695590267:web:7f443b818a06498882edde",
measurementId: "G-ZY75SZ2M0J"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
// Function to upload file from Local Storage to Firebase Storage
function uploadFileFromLocalStorage(storageRef, localStorageKey, targetElementId, buttonId) {
const fileData = localStorage.getItem(localStorageKey);
const button = document.getElementById('upld');
const urltt = document.getElementById("img_url")
if (fileData && button) {
// Disable the button while processing
button.disabled = true;
button.textContent = 'Processing...';
try {
// Upload file data to Firebase Storage
uploadString(storageRef, fileData, 'data_url')
.then((snapshot) => {
console.log('Uploaded file successfully:', snapshot.ref.fullPath);
// Add further logic (e.g., store download URL in database)
return getDownloadURL(snapshot.ref); // Return the promise for chaining
})
.then((url) => {
console.log('Download URL:', url);
// Set the download URL in the specified target element
urltt.value = url;
// Optionally, you can use the URL to fetch or display the file in your application
// Enable the button and update its text
button.textContent = 'Upload Complete';
});
} catch (error) {
button.textContent = 'Upload Failed';
}
} else {
console.error('No file data found in Local Storage for key:', localStorageKey);
}
}
var prod_name = "<?php echo $_SESSION['lastProductId'] + 1; ?>";
const storageRef = ref(storage, 'images/' + prod_name + '.txt'); // Specify the file path or name in Firebase Storage
const localStorageKey = 'capturedImage'; // Key used to store the file data in Local Storage
const targetElementId = 'img_url'; // ID of the target element to set the download URL
// const buttonId = 'upld'; // ID of the button to modify
document.getElementById('upld').addEventListener(
'click', () => {
uploadFileFromLocalStorage(storageRef, localStorageKey);
}
)
</script>
</html>