create.php 770 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. include '../connection.php';
  3. try {
  4. $params = json_decode(file_get_contents('php://input'));
  5. unset($params->id);
  6. $statement = $db->prepare('insert into task (title, list_id, due, reminder, done, note) values(:title, :list_id, :due, :reminder, :done, :note)');
  7. foreach($params as $key => &$value) {
  8. $statement->bindParam(":$key", $value);
  9. }
  10. if(!$statement->execute()) {
  11. throw new Exception(implode(', ', $statement->errorInfo()));
  12. }
  13. $params->id = $db->lastInsertId();
  14. $jsonResult = array(
  15. 'success' => true,
  16. 'tasks' => $params
  17. );
  18. } catch(Exception $e) {
  19. $jsonResult = array(
  20. 'success' => false,
  21. 'message' => $e->getMessage()
  22. );
  23. }
  24. echo json_encode($jsonResult);
  25. ?>