-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddSale.php
39 lines (34 loc) · 1.36 KB
/
addSale.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
<?php
include "Conx.php"; // Include database connection
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Content-Type: application/json');
// Handle POST request to add sale
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$product_id = $_POST['product_id'] ?? null;
$percentage = $_POST['percentage'] ?? null;
// Validate the input fields
if (!$product_id || !$percentage) {
echo json_encode([
'success' => false,
'message' => 'Both product ID and discount percentage are required.'
]);
exit;
}
// Insert sale data into makesale table
$stmt = $conn->prepare("INSERT INTO makesale (product_id, percentage) VALUES (?, ?)");
if ($stmt) {
$stmt->bind_param('ii', $product_id, $percentage);
if ($stmt->execute()) {
echo json_encode(['success' => true, 'message' => 'Sale added successfully.']);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to insert sale.', 'error' => $conn->error]);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => 'Failed to prepare the query', 'error' => $conn->error]);
}
}
$conn->close();
?>