-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts_sql.php
133 lines (112 loc) · 3.5 KB
/
products_sql.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
<?php
function getAllProduct() {
global $servername, $username, $password, $dbname;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
$sql = "SELECT * FROM shop";
$result = mysqli_query($conn, $sql);
$arary;
if (mysqli_num_rows($result) > 0) {
while($data = mysqli_fetch_assoc($result)) {
$arary[] = $data;
}
}
mysqli_close($conn);
return ($arary);
}
function getAllProductByName() {
global $servername, $username, $password, $dbname;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
$sql = "SELECT * FROM shop ORDER BY name";
$result = mysqli_query($conn, $sql);
$arary;
if (mysqli_num_rows($result) > 0) {
while($data = mysqli_fetch_assoc($result)) {
echo "id: " . $data["id"]. " - Name: " . $data["name"]. "<br>";
$arary[] = $data;
}
} else {
echo "0 results";
}
mysqli_close($conn);
return ($arary);
}
function getAllProductByPriceAsc() {
global $servername, $username, $password, $dbname;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
$sql = "SELECT * FROM shop ORDER BY price ASC";
$result = mysqli_query($conn, $sql);
$arary;
if (mysqli_num_rows($result) > 0) {
while($data = mysqli_fetch_assoc($result)) {
echo "id: " . $data["id"]. " - Name: " . $data["name"]. "<br>";
$arary[] = $data;
}
} else {
echo "0 results";
}
mysqli_close($conn);
return ($arary);
}
function getAllProductByPriceDesc() {
global $servername, $username, $password, $dbname;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
$sql = "SELECT * FROM shop ORDER BY price DESC";
$result = mysqli_query($conn, $sql);
$arary;
if (mysqli_num_rows($result) > 0) {
while($data = mysqli_fetch_assoc($result)) {
echo "id: " . $data["id"]. " - Name: " . $data["name"]. "<br>";
$arary[] = $data;
}
} else {
echo "0 results";
}
mysqli_close($conn);
return ($arary);
}
function putProduct ($name, $description, $price, $img, $category) {
global $servername, $username, $password, $dbname;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
if ($price < 0)
$price = 0;
if ($category < 0)
$category = 0;
$sql = "INSERT INTO shop (name, description, price, img, category_id)
VALUES ('$name', '$description', '$price', '$img', '$category')";
$res = false;
if (mysqli_query($conn, $sql)) {
$res = true;
}
mysqli_close($conn);
return ($res);
}
function deleteProduct ($id) {
global $servername, $username, $password, $dbname;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
$sql = "DELETE FROM shop WHERE id='$id'";
$res = false;
if (mysqli_query($conn, $sql)) {
$res = true;
}
mysqli_close($conn);
return ($res);
}
function updateProduct ($id, $name, $description, $price, $img) {
global $servername, $username, $password, $dbname;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
$sql = "UPDATE shop SET name='$name', description='$description', price=$price, img = '$img' WHERE id='$id'";
$res = false;
if (mysqli_query($conn, $sql)) {
$res = true;
}
mysqli_close($conn);
return ($res);
}
?>