No Description

index.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?PHP
  2. header('Access-Control-Allow-Origin: *');
  3. header('Access-Control-Allow-Headers: *');
  4. header('Access-Control-Allow-Methods: *');
  5. header('Content-Type: application/json; charset=UTF-8');
  6. //Database Connection
  7. $host = 'localhost';
  8. $dbname = 'censo';
  9. $user = 'luis.ortiz79';
  10. $passw = '27gnpXrDmPUPzEPQ';
  11. try{
  12. $connection = mysqli_connect($host, $user, $passw, $dbname) or $error = 1;
  13. }
  14. catch(Exception $ex){
  15. print("Error connecting to database: ". $ex->getMessage()) and die();
  16. }
  17. $method = $_SERVER['REQUEST_METHOD'];
  18. switch($method){
  19. case "GET":
  20. $sql = "SELECT * FROM noticias";
  21. $path = explode('/', $_SERVER['REQUEST_URI']);
  22. if(isset($path[3]) && is_numeric($path[3])) {
  23. $sql .= " WHERE id = :id";
  24. $stmt = $conn->prepare($sql);
  25. $stmt->bindParam(':id', $path[3]);
  26. $stmt->execute();
  27. $users = $stmt->fetch(PDO::FETCH_ASSOC);
  28. } else {
  29. $stmt = $conn->prepare($sql);
  30. $stmt->execute();
  31. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  32. }
  33. echo json_encode($users);
  34. break;
  35. case "POST":
  36. $user = json_decode(file_get_contents('php://input') );
  37. $sql = "INSERT INTO noticias(id, title, subject, created_at) VALUES(null, :title, :subject, :created_at)";
  38. $stmt = $conn->prepare($sql);
  39. $created_at = date('Y-m-d');
  40. $stmt->bindParam(':title', $user->title);
  41. $stmt->bindParam(':subject', $user->subject);
  42. $stmt->bindParam(':created_at', $created_at);
  43. if($stmt->execute()) {
  44. $response = ['status' => 1, 'message' => 'Record updated successfully.'];
  45. } else {
  46. $response = ['status' => 0, 'message' => 'Failed to update record.'];
  47. }
  48. echo json_encode($response);
  49. break;
  50. case "PUT":
  51. $user = json_decode( file_get_contents('php://input') );
  52. $sql = "UPDATE noticias SET title= :title, subject = :subject, updated_at = :updated_at WHERE id = :id";
  53. $stmt = $conn->prepare($sql);
  54. $updated_at = date('Y-m-d');
  55. $stmt->bindParam(':title', $user->title);
  56. $stmt->bindParam(':subject', $user->subject);
  57. $stmt->bindParam(':updated_at', $updated_at);
  58. if($stmt->execute()) {
  59. $response = ['status' => 1, 'message' => 'Record updated successfully.'];
  60. } else {
  61. $response = ['status' => 0, 'message' => 'Failed to update record.'];
  62. }
  63. echo json_encode($response);
  64. break;
  65. case "DELETE":
  66. $sql = "DELETE FROM noticias WHERE id = :id";
  67. $path = explode('/', $_SERVER['REQUEST_URI']);
  68. $stmt = $conn->prepare($sql);
  69. $stmt->bindParam(':id', $path[3]);
  70. if($stmt->execute()) {
  71. $response = ['status' => 1, 'message' => 'Record deleted successfully.'];
  72. } else {
  73. $response = ['status' => 0, 'message' => 'Failed to delete record.'];
  74. }
  75. echo json_encode($response);
  76. break;
  77. }
  78. ?>