-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_functions.php
229 lines (159 loc) · 6.9 KB
/
db_functions.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
<?php
error_reporting(0);
function createNewUser()
{
require 'db_config.php';
$user_id = uniqid("",true);
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
$query = "INSERT INTO `users` (`user_id`) VALUES ('".mysqli_escape_string($db_link, $user_id)."')";
$result = mysqli_query($db_link, $query);
if ($result) {
return $user_id;
} else {
return false;
}
}
function createNewOrder($userID)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
$query = "INSERT INTO `orders` (`order_id`,`user_id`) VALUES (NULL,'" . mysqli_escape_string($db_link, $userID) ."')";
$result = mysqli_query($db_link, $query);
if ($result) {
return mysqli_insert_id($db_link);
} else {
return false;
}
}
// Получить товары в корзине для пользователя
function getUserOrders($id)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
mysqli_query($db_link, "SET NAMES utf8");
$query = "SELECT `orders_content`.`product_id` , `goods`.`name` AS `product_name`, `goods`.`image` , `goods`.`parent_id`, `goods`.`cost` , `producers`.`name` , `orders_content`.`amount`
FROM `orders_content`
INNER JOIN `orders` ON `orders_content`.`order_id` = `orders`.`order_id`
INNER JOIN `goods` ON `orders_content`.`product_id` = `goods`.`product_id`
INNER JOIN `producers` ON `goods`.`producer_id` = `producers`.`producer_id`
WHERE `orders`.`user_id` = '" . mysqli_escape_string($db_link, $id)."'";
$result = mysqli_query($db_link, $query);
$rows = mysqli_affected_rows($db_link);
if ($rows > 0) {
return $result;
} elseif ($rows == 0) {
return false;
}
}
// Получить товары для добавления в корзину (уже добавленные в корзину($addedProductsIds) не должны отображаться)
function getProducts($addedProductsIds)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
mysqli_query($db_link, "SET NAMES utf8");
forEach ($addedProductsIds as $key => $value) {
$addedProductsIds[$key] = mysqli_escape_string($db_link, $value);
}
$idsForQuery = implode(',', $addedProductsIds);
if (count($addedProductsIds) > 0) {
$cond = " AND `goods`.`product_id` NOT IN (" . mysqli_escape_string($db_link, $idsForQuery) . ")";
} else {
$cond = "";
}
$query = "SELECT `goods`.`product_id` , `goods`.`parent_id`, `goods`.`name` AS `product_name`, `goods`.`image` , `goods`.`cost` , `producers`.`name`
FROM `goods`
INNER JOIN `producers` ON `goods`.`producer_id` = `producers`.`producer_id`
WHERE `goods`.`isGroup` = 0" . $cond;
$result = mysqli_query($db_link, $query);
$rows = mysqli_affected_rows($db_link);
if ($rows > 0) {
return $result;
} elseif ($rows == 0) {
return false;
}
}
function getOrderID($userID)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
mysqli_query($db_link, "SET NAMES utf8");
$query = "SELECT `orders`.`order_id` FROM `orders` WHERE `orders`.`user_id` = '". mysqli_escape_string($db_link, $userID)."'";
$result = mysqli_query($db_link, $query);
$rows = mysqli_affected_rows($db_link);
if ($rows > 0) {
$row = mysqli_fetch_assoc($result);
return $row['order_id'];
} elseif ($rows == 0) {
return false;
}
}
function addProductToOrder($orderID, $productID, $amount)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
$result = mysqli_query($db_link, "INSERT INTO `orders_content` (
`id` ,
`order_id` ,
`product_id` ,
`amount`
)
VALUES (
NULL , " . mysqli_escape_string($db_link, $orderID) . "," . mysqli_escape_string($db_link, $productID) . "," . mysqli_escape_string($db_link, $amount) . "
)");
if ($result) {
return true;
} else {
return false;
}
}
function removeProductFromOrder($orderID, $productID)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
$query = "DELETE FROM `orders_content` WHERE `orders_content`.`order_id` = " . mysqli_escape_string($db_link, $orderID) . " AND `orders_content`.`product_id` = " . mysqli_escape_string($db_link, $productID);
$result = mysqli_query($db_link, $query);
if ($result) {
return true;
} else {
return false;
}
}
function updateAmountInOrder($orderID, $productID, $newAmount)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
$query = "UPDATE `orders_content` SET `amount` = " . mysqli_escape_string($db_link, $newAmount) . " WHERE `orders_content`.`order_id` = " . mysqli_escape_string($db_link, $orderID) . " AND `orders_content`.`product_id` = " . mysqli_escape_string($db_link, $productID);
$result = mysqli_query($db_link, $query);
if ($result) {
return true;
} else {
return false;
}
}
function formCategoryTree($productID){
$categories = array();
$productInfo = getProcuctById($productID);
while ($productInfo['parent_id'] !== null) {
$productInfo = getProcuctById($productInfo['parent_id']);
array_push($categories, $productInfo['name']);
}
return array_reverse($categories);
}
function getProcuctById($productID)
{
require 'db_config.php';
$db_link = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
mysqli_query($db_link, "SET NAMES utf8");
$query = "SELECT `goods`.`name` , `goods`.`parent_id`
FROM `goods`
WHERE `goods`.`product_id` = ".mysqli_escape_string($db_link, $productID);
$result = mysqli_query($db_link, $query);
$rows = mysqli_affected_rows($db_link);
if ($rows > 0) {
$row = mysqli_fetch_assoc($result);
return $row;
} elseif ($rows == 0) {
return false;
}
}
?>