add-owner-to-collaborators.php 1.8 KB

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