-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
41 lines (30 loc) · 1.05 KB
/
db.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
<?php
require('db-info.php');
$query = '';
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$query = "SELECT * FROM `holders` LIMIT 0 , 30";
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$postData = ['name' => $_POST['name'], 'current' => $_POST['current']];
$query = 'INSERT INTO `holders`
(`name`, `current`)
VALUES
('.$postData["new"]["name"] . ', ' . $postData["new"]["current"] . '),
('.$postData["old"]["name"] . ', ' . $postData["old"]["current"] . ')
ON DUPLICATE KEY UPDATE
SET `current` = {$postData["current"]} WHERE `name` = {$postData["name"]}';
}
try {
$conn = new PDO('mysql:host='.$host.';dbname='.$db, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->query($query);
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
foreach($data as $row) {
print_r(json_encode($row)."|");
}
} else {
echo '{"status": "success"}';
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>