-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
147 lines (99 loc) · 2.97 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Server Connection String
*/
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_notes";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['ACTION']) && $_POST['ACTION']=='add_note'){
$sql = "INSERT INTO notes (`note`)
VALUES ('".$_POST["note"]."')";
if ($conn->query($sql) === TRUE) {
$sql = "SELECT `id`, `note`, `timestamp` FROM `notes`";
$result = $conn -> query($sql);
// Fetch all
$resultSet=$result -> fetch_all(MYSQLI_ASSOC);
// Free result set
//$result -> free_result();
echo json_encode($resultSet);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
if(isset($_POST['ACTION']) && $_POST['ACTION']=='get_all_notes'){
$sql = "SELECT `id`, `note`, `timestamp` FROM `notes`";
$result = $conn -> query($sql);
// Fetch all
$resultSet=$result -> fetch_all(MYSQLI_ASSOC);
// Free result set
//$result -> free_result();
echo json_encode($resultSet);
$conn -> close();
}
if(isset($_POST['ACTION']) && $_POST['ACTION']=='delete_note'){
$id = $_POST["id"];
$sql = "DELETE FROM `notes` WHERE `id`='$id'";
if ($conn->query($sql) === TRUE) {
$sql = "SELECT `id`, `note`, `timestamp` FROM `notes`";
$result = $conn -> query($sql);
// Fetch all
$resultSet=$result -> fetch_all(MYSQLI_ASSOC);
echo json_encode($resultSet);
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn -> close();
}
if(isset($_POST['ACTION']) && $_POST['ACTION']=='update_note'){
$id = $_POST["id"];
$newVal = $_POST["note"];
$sql = "UPDATE `notes` SET `note`='$newVal' WHERE `id`='$id'";
if ($conn->query($sql) === TRUE) {
$sql = "SELECT `id`, `note`, `timestamp` FROM `notes`";
$result = $conn -> query($sql);
// Fetch all
$resultSet=$result -> fetch_all(MYSQLI_ASSOC);
echo json_encode($resultSet);
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn -> close();
}
// if(isset($_POST['ACTION']) && $_POST['ACTION']=='update_note'){
// $sql = "UPDATE `notes` SET `note`='".$_POST["note"]."' WHERE `id`='".$_POST["id]."'";
// $result = $conn -> query($sql);
// // Fetch all
// $resultSet=$result -> fetch_all(MYSQLI_ASSOC);
// // Free result set
// //$result -> free_result();
// echo json_encode($resultSet);
// $conn -> close();
// }
// if(isset($_POST['ACTION']) && $_POST['ACTION']=='delete_note')
// {
// $id = $_POST["id"];
// $sql = "DELETE FROM `notes` WHERE `id`='$id'";
// $result = $conn -> query($sql);
// // Fetch all
// $resultSet=$result -> fetch_all(MYSQLI_ASSOC);
// // Free result set
// //$result -> free_result();
// echo json_encode($resultSet);
// $conn -> close();
// }
?>