-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-add-ajax.php
118 lines (99 loc) · 3.61 KB
/
product-add-ajax.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
<?php
function productCreated(){
if($_POST['action'] == 'productCreated' ){
$productName = $_POST['productName'] ?? null;
$productCategory = $_POST['category'] ?? null;
$productPrice = $_POST['cost'] ?? null;
$error = false;
if( empty($productName) ){
$error = true;
$response['error'] = true;
$response['productName'] = 'Product name is required!';
}
if( empty($productCategory) ){
$error = true;
$response['error'] = true;
$response['category'] = 'Product category is required!';
}
if( empty($productPrice) ){
$error = true;
$response['error'] = true;
$response['cost'] = 'Product price is required!';
}
if($error){
echo json_encode($response);
exit;
}
include_once 'databaseConnection.php';
$db = new \DB\DBConnection(); // Use namespace
$mysqli = $db->dbConnect();
$data = [];
$stmt = $mysqli->prepare("INSERT INTO products(product_name,category, product_price) VALUES (?, ?, ?)");
$stmt->bind_param("ssd", $productName, $productCategory, $productPrice);
// Execute the statement
$saveProduct = $stmt->execute();
$allProducts = $mysqli->query("SELECT * FROM products");
if ($allProducts->num_rows > 0) {
while($row = $allProducts->fetch_assoc()) {
$data[] = $row;
}
}
// Close the statement
$stmt->close();
echo json_encode([
'message' => 'Successfully added.',
'response' => $data,
'status' => true,
'error' => false,
]);
}
if($_POST['action'] == 'productUpdated' ){
$productId = $_POST['product_id'] ?? null;
$productName = $_POST['productName'] ?? null;
$productCategory = $_POST['category'] ?? null;
$productPrice = $_POST['cost'] ?? null;
$error = false;
if( empty($productName) ){
$error = true;
$response['error'] = true;
$response['productName'] = 'Product name is required!';
}
if( empty($productCategory) ){
$error = true;
$response['error'] = true;
$response['category'] = 'Product category is required!';
}
if( empty($productPrice) ){
$error = true;
$response['error'] = true;
$response['cost'] = 'Product price is required!';
}
if($error){
echo json_encode($response);
exit;
}
include_once 'databaseConnection.php';
$db = new \DB\DBConnection(); // Use namespace
$mysqli = $db->dbConnect();
$data = [];
$stmt = $mysqli->prepare("UPDATE products SET product_name = ?, category = ?, product_price = ? WHERE product_id = ?");
$stmt->bind_param("ssdi", $productName, $productCategory, $productPrice, $productId);
// Execute the statement
$saveProduct = $stmt->execute();
$allProducts = $mysqli->query("SELECT * FROM products");
if ($allProducts->num_rows > 0) {
while($row = $allProducts->fetch_assoc()) {
$data[] = $row;
}
}
// Close the statement
$stmt->close();
echo json_encode([
'message' => 'Successfully added.',
'response' => $data,
'status' => true,
'error' => false,
]);
}
}
productCreated();