Advertisement
Guest User

Untitled

a guest
Sep 15th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. $serverName = "localhost";
  5. $username = "root";
  6. $password = "";
  7. $dbname = "note_db";
  8.  
  9. //Create connection
  10. $conn = new mysqli($serverName, $username, $password, $dbname);
  11.  
  12. //Check connection
  13. if($conn->connect_error){
  14. $json = [
  15. 'header' =>
  16. [
  17. 'msg' => "Connection failed: " . $conn->connect_error,
  18. 'code' => 500,
  19. ],
  20. 'body' => []
  21. ];
  22. echo json_encode($json, JSON_PRETTY_PRINT);
  23. die();
  24. }
  25.  
  26. echo "Connected Successfully";
  27.  
  28. $method = $_SERVER['REQUEST_METHOD'];
  29.  
  30. if($method == 'PUT'){
  31. //Create Note
  32. parse_str(file_get_contents( 'php://input' ),$_REQUEST);
  33.  
  34. $title = $_REQUEST['title'];
  35. $content = $_REQUEST['content'];
  36.  
  37. $sql = "INSERT INTO `notes`(`title`, `content`, `created_date`, `modified_date`) VALUES ('$title','$content',NOW(),NOW())";
  38.  
  39. if($conn->query($sql) === TRUE) {
  40. $json =
  41. [
  42. 'header' =>
  43. [
  44. 'msg' => "New record created successfully",
  45. 'code' => 201
  46. ],
  47. 'body' => [
  48. 'id' => $conn->insert_id
  49. ]
  50. ];
  51. echo json_encode($json, JSON_PRETTY_PRINT);
  52. }
  53. else {
  54. $json =
  55. [
  56. 'header' =>
  57. [
  58. 'msg' => "New record created successfully",
  59. 'code' => 201
  60. ],
  61. 'body' => []
  62. ];
  63. echo json_encode($json, JSON_PRETTY_PRINT);
  64.  
  65. }
  66. $conn->close();
  67. }
  68.  
  69. else if ($method === 'POST'){
  70. // Update Note
  71. $id = $_REQUEST['id'];
  72. $title = $_REQUEST['title'];
  73. $content = $_REQUEST['content'];
  74.  
  75. $sql = "UPDATE `notes` SET `id`=$id,`title`='$title',`content`='$content',`modified_date`=NOW() WHERE id='$id'";
  76.  
  77. if ($conn->query($sql) === TRUE)
  78. {
  79. $json =
  80. [
  81. 'header' =>
  82. [
  83. 'msg' => "Record updated successfully",
  84. 'code' => 200
  85. ],
  86. 'body' => []
  87. ];
  88. echo json_encode($json, JSON_PRETTY_PRINT);
  89. }
  90. else {
  91.  
  92. $json =
  93. [
  94. 'header' =>
  95. [
  96. 'msg' => $conn->error,
  97. 'code' => 400
  98. ],
  99. 'body' => []
  100. ];
  101. echo json_encode($json, JSON_PRETTY_PRINT);
  102.  
  103. }
  104. $conn->close();
  105. }
  106.  
  107. else if ($method === 'DELETE')
  108. {
  109. //Delete Note
  110. $id=$_REQUEST['id'];
  111. $sql = "DELETE FROM `notes` WHERE id=$id";
  112.  
  113. if ($conn->query($sql) === TRUE)
  114. {
  115. echo "Record deleted successfully";
  116. $json =
  117. [
  118. 'header' =>
  119. [
  120. 'msg' => "Record deleted successfully",
  121. 'code' => 204
  122. ],
  123. 'body' => []
  124. ];
  125. echo json_encode($json, JSON_PRETTY_PRINT);
  126.  
  127. }
  128. else {
  129. $json =
  130. [
  131. 'header' =>
  132. [
  133. 'msg' => $conn->error,
  134. 'code' => 400
  135. ],
  136. 'body' => []
  137. ];
  138. echo json_encode($json, JSON_PRETTY_PRINT);
  139.  
  140. }
  141. $conn->close();
  142. }
  143.  
  144. else if ($method === 'GET')
  145. {
  146. $sql = "";
  147.  
  148. if(empty($_REQUEST['id']))
  149. {
  150. // GET All Notes
  151. $sql = "SELECT * FROM notes ORDER BY created_date DESC";
  152. }
  153. else
  154. {
  155. //Get one Note
  156. $id = $_REQUEST['id'];
  157. $sql = "SELECT * FROM notes WHERE id=$id";
  158. }
  159.  
  160. $result = $conn->query($sql);
  161.  
  162. if($result->num_rows > 0)
  163. {
  164. $body = array();
  165. //output data for each row
  166. while($row = $result->fetch_assoc())
  167. {
  168. array_push($body, $row);
  169. }
  170.  
  171. $json =
  172. [
  173. 'header' =>
  174. [
  175. 'msg' => "OK - Everything is working",
  176. 'code' => 200
  177. ],
  178. 'body' => $body
  179. ];
  180. echo json_encode($json, JSON_PRETTY_PRINT);
  181. }
  182. else
  183. {
  184. $json =
  185. [
  186. 'header' =>
  187. [
  188. 'msg' => $conn->error,
  189. 'code' => 400
  190. ],
  191. 'body' => []
  192. ];
  193. echo json_encode($json, JSON_PRETTY_PRINT);
  194. }
  195. $conn->close();
  196. }
  197.  
  198.  
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement