-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakesale.php
49 lines (41 loc) · 1.8 KB
/
Makesale.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
<?php
include "Conx.php";
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Methods: OPTIONS, GET, POST');
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$sql = "SELECT id, name FROM products";
$result = $conn->query($sql);
if ($result) {
$products = [];
while ($row = $result->fetch_assoc()) {
$products[] = $row;
}
echo json_encode(['success' => true, 'products' => $products]);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to retrieve products', 'error' => $conn->error]);
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents("php://input"), true);
if (isset($data['productId'], $data['percentage'], $data['startDate'], $data['endDate'])) {
$productId = $data['productId'];
$percentage = $data['percentage'];
$startDate = $data['startDate'];
$endDate = $data['endDate'];
$stmt = $conn->prepare("INSERT INTO makesale (percentage, start_date, end_date, product_id) VALUES (?, ?, ?, ?)");
$stmt->bind_param("dssi", $percentage, $startDate, $endDate, $productId);
if ($stmt->execute()) {
echo json_encode(['success' => true, 'message' => 'Sale created successfully!']);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to create sale', 'error' => $stmt->error]);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => 'Invalid input data']);
}
} else {
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
}
$conn->close();
?>