-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtasks.php
32 lines (26 loc) · 947 Bytes
/
tasks.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
<?php
$db = new PDO("mysql:host=localhost;dbname=tasks", "root", "root");
$data = json_decode(file_get_contents('php://input'));
if ($_SERVER['REQUEST_METHOD'] == "GET"){
$statement = $db->query('SELECT * FROM tasks');
$statement->setFetchMode(PDO::FETCH_ASSOC);
echo json_encode($statement->fetchAll());
}
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$sql = "INSERT INTO tasks (title) values (:title)";
$query = $db->prepare($sql);
$query->execute(array(":title"=>$data->title));
$result['id'] = $db->lastInsertId();
echo json_encode($result);
}
if ($_SERVER['REQUEST_METHOD'] == "PUT"){
$sql = "UPDATE tasks SET done = :done WHERE id = :id";
$query = $db->prepare($sql);
$query->execute(array(":done"=>$data->done, ":id"=>$data->id));
}
if ($_SERVER['REQUEST_METHOD'] == "DELETE"){
$sql = "DELETE FROM tasks WHERE id = :id";
$query = $db->prepare($sql);
$query->execute(array(":id"=>$_GET['id']));
}
?>