├── README.md ├── koneksi.php └── restapi.php /README.md: -------------------------------------------------------------------------------- 1 | # CRUD Rest API 2 | 3 | Create a CRUD Rest API with PHP and MySQL 4 | 5 | ## Quick Start 6 | 7 | Follow the steps below to run the project. 8 | 9 | 1. Clone Repository 10 | 2. Add Project to Folder `C:\xampp\htdocs` 11 | 3. Run Project in Browser 12 | 13 | ## Connect with Us 14 | 15 | Website: [Zelixify](https://zelixify.web.id) 16 | Like us on [Instagram](https://www.instagram.com/zelixify) 17 | Follow us on [Twitter](https://twitter.com/danendranr) 18 | -------------------------------------------------------------------------------- /koneksi.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /restapi.php: -------------------------------------------------------------------------------- 1 | query("SELECT * FROM note"); 13 | while ($row = mysqli_fetch_object($query)) { 14 | $data[] = $row; 15 | } 16 | $response = array( 17 | 'status' => 200, 18 | 'message' => 'success', 19 | 'author' => 'Danendra Coding', 20 | 'data' => $data 21 | ); 22 | header('Content-Type: application/json'); 23 | echo json_encode($response); 24 | } 25 | 26 | function getNoteId() 27 | { 28 | global $koneksi; 29 | if (!empty($_GET["id"])) { 30 | $id = $_GET["id"]; 31 | } 32 | $query = "SELECT * FROM note WHERE id= $id"; 33 | $result = $koneksi->query($query); 34 | while ($row = mysqli_fetch_object($result)) { 35 | $data[] = $row; 36 | } 37 | if ($data) { 38 | $response = array( 39 | 'status' => 200, 40 | 'message' => 'success', 41 | 'author' => 'Danendra Coding', 42 | 'data' => $data 43 | ); 44 | } else { 45 | $response = array( 46 | 'status' => 404, 47 | 'message' => 'error' 48 | ); 49 | } 50 | 51 | header('Content-Type: application/json'); 52 | echo json_encode($response); 53 | } 54 | 55 | function addNote() 56 | { 57 | global $koneksi; 58 | $check = array('judul' => '', 'catatan' => '', 'tanggal' => ''); 59 | $check_match = count(array_intersect_key($_POST, $check)); 60 | if ($check_match == count($check)) { 61 | 62 | $result = mysqli_query($koneksi, "INSERT INTO note SET 63 | judul = '$_POST[judul]', 64 | catatan = '$_POST[catatan]', 65 | tanggal ='$_POST[tanggal]'"); 66 | 67 | if ($result) { 68 | $response = array( 69 | 'status' => 200, 70 | 'message' => 'success' 71 | ); 72 | } else { 73 | $response = array( 74 | 'status' => 404, 75 | 'message' => 'error' 76 | ); 77 | } 78 | } else { 79 | $response = array( 80 | 'status' => 404, 81 | 'message' => 'error' 82 | ); 83 | } 84 | header('Content-Type: application/json'); 85 | echo json_encode($response); 86 | } 87 | 88 | function updateNote() 89 | { 90 | global $koneksi; 91 | if (!empty($_GET["id"])) { 92 | $id = $_GET["id"]; 93 | } 94 | $check = array('judul' => '', 'catatan' => '', 'tanggal' => ''); 95 | $check_match = count(array_intersect_key($_POST, $check)); 96 | if ($check_match == count($check)) { 97 | 98 | $result = mysqli_query($koneksi, "UPDATE note SET 99 | judul = '$_POST[judul]', 100 | catatan = '$_POST[catatan]', 101 | tanggal = '$_POST[tanggal]' WHERE id = $id"); 102 | 103 | if ($result) { 104 | $response = array( 105 | 'status' => 200, 106 | 'message' => 'success' 107 | ); 108 | } else { 109 | $response = array( 110 | 'status' => 404, 111 | 'message' => 'error' 112 | ); 113 | } 114 | } else { 115 | $response = array( 116 | 'status' => 404, 117 | 'message' => 'error', 118 | 'data' => $id 119 | ); 120 | } 121 | header('Content-Type: application/json'); 122 | echo json_encode($response); 123 | } 124 | 125 | function deleteNote() 126 | { 127 | global $koneksi; 128 | $id = $_GET['id']; 129 | $query = "DELETE FROM note WHERE id=" . $id; 130 | if (mysqli_query($koneksi, $query)) { 131 | $response = array( 132 | 'status' => 200, 133 | 'message' => 'success' 134 | ); 135 | } else { 136 | $response = array( 137 | 'status' => 404, 138 | 'message' => 'error' 139 | ); 140 | } 141 | header('Content-Type: application/json'); 142 | echo json_encode($response); 143 | } 144 | 145 | ?> --------------------------------------------------------------------------------