Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $serverName = "localhost";
- $username = "root";
- $password = "";
- $dbname = "note_db";
- //Create connection
- $conn = new mysqli($serverName, $username, $password, $dbname);
- //Check connection
- if($conn->connect_error){
- $json = [
- 'header' =>
- [
- 'msg' => "Connection failed: " . $conn->connect_error,
- 'code' => 500,
- ],
- 'body' => []
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- die();
- }
- echo "Connected Successfully";
- $method = $_SERVER['REQUEST_METHOD'];
- if($method == 'PUT'){
- //Create Note
- parse_str(file_get_contents( 'php://input' ),$_REQUEST);
- $title = $_REQUEST['title'];
- $content = $_REQUEST['content'];
- $sql = "INSERT INTO `notes`(`title`, `content`, `created_date`, `modified_date`) VALUES ('$title','$content',NOW(),NOW())";
- if($conn->query($sql) === TRUE) {
- $json =
- [
- 'header' =>
- [
- 'msg' => "New record created successfully",
- 'code' => 201
- ],
- 'body' => [
- 'id' => $conn->insert_id
- ]
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- else {
- $json =
- [
- 'header' =>
- [
- 'msg' => "New record created successfully",
- 'code' => 201
- ],
- 'body' => []
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- $conn->close();
- }
- else if ($method === 'POST'){
- // Update Note
- $id = $_REQUEST['id'];
- $title = $_REQUEST['title'];
- $content = $_REQUEST['content'];
- $sql = "UPDATE `notes` SET `id`=$id,`title`='$title',`content`='$content',`modified_date`=NOW() WHERE id='$id'";
- if ($conn->query($sql) === TRUE)
- {
- $json =
- [
- 'header' =>
- [
- 'msg' => "Record updated successfully",
- 'code' => 200
- ],
- 'body' => []
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- else {
- $json =
- [
- 'header' =>
- [
- 'msg' => $conn->error,
- 'code' => 400
- ],
- 'body' => []
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- $conn->close();
- }
- else if ($method === 'DELETE')
- {
- //Delete Note
- $id=$_REQUEST['id'];
- $sql = "DELETE FROM `notes` WHERE id=$id";
- if ($conn->query($sql) === TRUE)
- {
- echo "Record deleted successfully";
- $json =
- [
- 'header' =>
- [
- 'msg' => "Record deleted successfully",
- 'code' => 204
- ],
- 'body' => []
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- else {
- $json =
- [
- 'header' =>
- [
- 'msg' => $conn->error,
- 'code' => 400
- ],
- 'body' => []
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- $conn->close();
- }
- else if ($method === 'GET')
- {
- $sql = "";
- if(empty($_REQUEST['id']))
- {
- // GET All Notes
- $sql = "SELECT * FROM notes ORDER BY created_date DESC";
- }
- else
- {
- //Get one Note
- $id = $_REQUEST['id'];
- $sql = "SELECT * FROM notes WHERE id=$id";
- }
- $result = $conn->query($sql);
- if($result->num_rows > 0)
- {
- $body = array();
- //output data for each row
- while($row = $result->fetch_assoc())
- {
- array_push($body, $row);
- }
- $json =
- [
- 'header' =>
- [
- 'msg' => "OK - Everything is working",
- 'code' => 200
- ],
- 'body' => $body
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- else
- {
- $json =
- [
- 'header' =>
- [
- 'msg' => $conn->error,
- 'code' => 400
- ],
- 'body' => []
- ];
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- $conn->close();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement