-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.php
76 lines (72 loc) · 2.41 KB
/
process.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
<?php
include ('database.php');
header("Content-Type: application/json");
function read($mysqli,$id){
if(isset($id)){
//echo $id;die();
$query = "SELECT * FROM `table` WHERE `id`= $id ;";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
}
else{
$query = "SELECT * FROM `table` ORDER BY dc DESC LIMIT 15;";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
}
$jsonData = "{";
while($row = $result->fetch_assoc()){
$jsonData .='"'.$row['id'].'":{"task":"'.$row['task'].'","completed":"'.$row['completed'].'"},';
}
$jsonData = chop($jsonData,",");
$jsonData .="}";
echo $jsonData;
}
function delete($id,$mysqli){
$delete_query = "DELETE FROM `table` WHERE `id` = $id";
$result = $mysqli->query($delete_query) or die($mysqli->error.__LINE__);
}
function insert($input,$mysqli){
$string = mysqli_real_escape_string($mysqli,$input);
$insert_query = "INSERT INTO `table` (task,dc) VALUES('$string',NOW());";
$mysqli->query($insert_query) or die($mysqli->error.__LINE__);
$last_id = mysqli_insert_id($mysqli);
return $last_id;
}
function update($input,$id,$mysqli){
$update_query = "UPDATE `table` SET `task` = '$input' , `dc` = NOW() WHERE `id` = $id ";
$result = $mysqli->query($update_query) or die($mysqli->error.__LINE__);
}
function complete($id,$mysqli){
$complete_query = "UPDATE `table` SET `completed` = 1 WHERE `id` = $id ";
$result = $mysqli->query($complete_query) or die($mysqli->error.__LINE__);
}
function incomplete($id,$mysqli){
$incomplete_query = "UPDATE `table` SET `completed` = 0 WHERE `id` = $id ";
$result = $mysqli->query($incomplete_query) or die($mysqli->error.__LINE__);
}
if ($_POST) {
switch ($_POST['operation']) {
case "insert":
read($mysqli,insert($_POST['input'],$mysqli));
break;
case "complete":
complete($_POST['id'],$mysqli);
echo '{"0":"COMLETED"}';
break;
case "incomplete":
incomplete($_POST['id'],$mysqli);
echo '{"0":"INCOMPLETE"}';
break;
case "update":
//echo '{"INPUT":"'.$_POST['input'].'","ID":"'.$_POST['id'].'"}';die();
update($_POST['input'],$_POST['id'],$mysqli);
echo '{"0":"UPDATED"}';
break;
case "delete":
delete($_POST['id'],$mysqli);
echo '{"0":"DELETED"}';
break;
default:
read($mysqli,null);
break;
}
}
?>