-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathserver.php
More file actions
76 lines (68 loc) · 1.95 KB
/
Copy pathserver.php
File metadata and controls
76 lines (68 loc) · 1.95 KB
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
$dsn = "mysql:host=localhost;dbname=wftutorials";
$user = "root";
$passwd = "";
$conn = new PDO($dsn, $user, $passwd);
function rowItem($id){
return
" <a data-id='".$id."' class='remove-task' href='javascript:void(0);'>×</a>";
}
if($_GET["action"] == 'get_items'){
$data = "";
try {
$results = $conn->query("SELECT * from my_tasks");
foreach($results as $result){
$data .= "<li>";
$data .= $result["name"];
$data .= rowItem($result["id"]);
$data .= "</li>";
}
echo $data;
}catch (Exception $e){
echo $e->getMessage();
}
}
if($_GET["action"] == 'save_task'){
$task = isset($_POST['task']) ? $_POST['task'] : null;
if(!empty($task)){
try{
$sql = "INSERT INTO my_tasks(`name`) VALUES (?)";
$query = $conn->prepare($sql);
$query->execute([$task]);
echo "good";
}catch (Exception $e){
echo $e->getMessage();
}
}
}
if($_GET["action"] == 'search_items'){
$data = "";
$query = isset($_GET['query']) ? $_GET['query'] : null;
try{
$db = $conn->prepare("SELECT * FROM my_tasks WHERE name LIKE ?");
$db->execute(["%" . $query . "%"]);
$results = $db->fetchAll();
foreach ($results as $result){
$data .= "<li>";
$data .= $result["name"];
$data .= rowItem($result["id"]);
$data .= "</li>";
}
echo $data;
}catch (Exception $e){
echo $e->getMessage();
}
}
if($_GET["action"] == 'remove_task'){
$taskId = isset($_POST['id']) ? $_POST['id'] : null;
if(!empty($taskId)){
try{
$sql = "DELETE from my_tasks WHERE id=?";
$query = $conn->prepare($sql);
$query->execute([$taskId]);
echo "good";
}catch (Exception $e){
echo $e->getMessage();
}
}
}