SHARE
TWEET

Untitled

a guest Jun 20th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. require '../vendor/autoload.php';
  3.  
  4. Flight::register('db', 'PDO', array('mysql:host=localhost:3306;dbname=studenti','root',''));
  5.  
  6. Flight::route('/', function(){
  7.     echo 'hello world!';
  8. });
  9.  
  10. // return all students info from database
  11. Flight::route('GET /students', function(){
  12.     $students = Flight::db()->query('SELECT * FROM students', PDO::FETCH_ASSOC)->fetchAll();
  13.     Flight::json($students);
  14. });
  15.  
  16. // add student to database
  17. Flight::route('POST /student', function(){
  18.     $request = Flight::request()->data->getData();
  19.     $insert = "INSERT INTO students (name, surname, birth_date, address, phone_number) VALUES(:name, :surname, :birth_date, :address, :phone_number)";
  20.     $stmt= Flight::db()->prepare($insert);
  21.     $stmt->execute($request);
  22. });
  23.  
  24. // return all courses info from database
  25. Flight::route('GET /courses', function(){
  26.     $courses = Flight::db()->query('SELECT * FROM courses', PDO::FETCH_ASSOC)->fetchAll();
  27.     Flight::json($courses);
  28. });
  29.  
  30. // add course to database
  31. Flight::route('POST /course', function(){
  32.     $request = Flight::request()->data->getData();
  33.     $insert = "INSERT INTO courses (name, lecture_hours, practice_hours, ects) VALUES(:name, :lecture_hours, :practice_hours, :ects)";
  34.     $stmt= Flight::db()->prepare($insert);
  35.     $stmt->execute($request);
  36. });
  37.  
  38. // return all courses from database for selected student
  39. Flight::route('GET /student_courses/@student_id', function($id){
  40.     $courses = Flight::db()->query('SELECT course FROM student-course WHERE id=:id', PDO::FETCH_ASSOC)->fetchAll();
  41.     Flight::json($courses);
  42. });
  43.  
  44. // add student course to database
  45.  
  46.  
  47. //  remove student course from database
  48. Flight::route('DELETE /student_course/@id', function($id){
  49.     $delete = "DELETE FROM student_course WHERE id = :id";
  50.     $stmt= Flight::db()->prepare($delete);
  51.     $stmt->execute([":id" => $id]);
  52. });
  53.  
  54. Flight::start();
  55. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top