add-collaborator.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. require_once("../../php/db_utils.php");
  3. try {
  4. // Declarations
  5. $collaboratorIsMember = false;
  6. $collaboratorExists = false;
  7. // Connect to the database
  8. $database = db_connect();
  9. // Incoming data
  10. $canvasId = $_POST["canvas_id"];
  11. $collaborator = $_POST["collaborator"];
  12. // Check if the collaborator is a member
  13. $query = 'SELECT username FROM user WHERE username = "' . $collaborator . '";';
  14. $recordSet = db_query_return($database, $query);
  15. $record = mysqli_fetch_assoc($recordSet);
  16. if(isset($record["username"])) {
  17. $collaboratorIsMember = true;
  18. }
  19. mysqli_free_result($recordSet);
  20. // Check if the collaborator already exists
  21. $query = 'SELECT * FROM collaborators WHERE canvas_id = "' . $canvasId . '" AND collaborator = "' . $collaborator . '";';
  22. $recordSet = db_query_return($database, $query);
  23. $record = mysqli_fetch_assoc($recordSet);
  24. if(isset($record["collaborator"])) {
  25. $collaboratorExists = true;
  26. }
  27. mysqli_free_result($recordSet);
  28. // If the collaborator is not already a member
  29. if($collaboratorIsMember == false) {
  30. echo(1);
  31. }
  32. // If the collaborator is already a collaborator
  33. else if($collaboratorExists == true) {
  34. echo(2);
  35. }
  36. else if($collaboratorIsMember == true && $collaboratorExists == false) {
  37. // Add collaborator
  38. $query = 'INSERT INTO collaborators (canvas_id, collaborator) VALUES ("' . $canvasId . '", "' . $collaborator . '");';
  39. echo(3);
  40. }
  41. // Insert new or update old description
  42. db_query_no_return($database, $query);
  43. // Close the connection to the database
  44. db_close($database);
  45. }
  46. catch(Exception $e) {
  47. $errorMsg = $e->getMessage();
  48. print_r($errorMsg);
  49. }
  50. ?>