-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddproduct.php
44 lines (37 loc) · 1.79 KB
/
addproduct.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
<?php
include "Conx.php";
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Methods: POST');
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['name']) && isset($_POST['description']) && isset($_POST['price']) && isset($_POST['quantity']) && isset($_FILES['image'])) {
$name = $_POST['name'];
$description = $_POST['description'];
$price = $_POST['price'];
$quantity = $_POST['quantity']; // Get the quantity value
$imageFile = $_FILES['image'];
$uploadDir = 'uploads/';
$imagePath = $uploadDir . basename($imageFile['name']);
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
if (move_uploaded_file($imageFile['tmp_name'], $imagePath)) {
// Insert the new product including the quantity field
$stmt = $conn->prepare("INSERT INTO products (name, description, price, quantity, image) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("ssdis", $name, $description, $price, $quantity, $imagePath);
if ($stmt->execute()) {
echo json_encode(['success' => true, 'message' => 'Product added successfully!', 'productId' => $stmt->insert_id]);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to add product', 'error' => $stmt->error ?: $conn->error]);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => 'Failed to upload image']);
}
} else {
echo json_encode(['success' => false, 'message' => 'Invalid input data']);
}
}
$conn->close();
?>