-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_context.php
70 lines (58 loc) · 1.92 KB
/
db_context.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
<?php
$server_name = "localhost";
$nser_name = "G08";
$password = "em56ping";
$db_name = "g08";
// Create connection
$conn = mysqli_connect($server_name, $nser_name, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$obj = json_decode($_POST["limit"], false);
$obj = json_decode(file_get_contents('php://input'), true);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
$query = "INSERT INTO buecher1 (" .
"isbn13, title, authors, publisher, price, stock, `desc`, pages, image" .
") VALUES (" .
"\"" . $obj["isbn13"] . "\"," .
"\"" . $obj["title"] . "\"," .
"\"" . $obj["authors"] . "\"," .
"\"" . $obj["publisher"] . "\"," .
substr($obj["price"], 1) . "," .
$obj["stock"] . "," .
"\"" . $obj["desc"] . "\"," .
$obj["pages"] . "," .
"\"" . $obj["image"] . "\")";
$stmt = $conn->prepare($query);
if (!$stmt) {
http_response_code(500);
echo json_encode($query);
} else {
$stmt->execute();
}
} catch (\Throwable $th) {
echo $th;
}
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$query = "SELECT * FROM buecher1";
$stmt = $conn->prepare($query);
//$stmt->bind_param("s", $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
$outp = $result->fetch_all(MYSQLI_ASSOC);
header("Content-Type: application/json; charset=UTF-8");
echo json_encode($outp);
} else if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
$query = "UPDATE buecher1 SET stock = " . $obj["stock"] . " WHERE isbn13 = " . $obj["isbn13"];
$stmt = $conn->prepare($query);
if (!$stmt) {
http_response_code(500);
echo json_encode($query);
} else {
$stmt->execute();
echo json_encode($query);
}
}
$conn->close();