save-tag.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. require_once("../../php/db_utils.php");
  3. try {
  4. // Connect to the database
  5. $database = db_connect();
  6. // Incoming data
  7. $tag = $_POST["tag"];
  8. $description = $_POST["description"];
  9. $canvasId = $_POST["canvas_id"];
  10. // Check if the tag already exists
  11. $query = 'SELECT tag FROM tags WHERE tag = "' . $tag . '" AND canvas_id = "' . $canvasId . '";';
  12. $recordSet = db_query_return($database, $query);
  13. $record = mysqli_fetch_assoc($recordSet);
  14. $tagCheck = $record["tag"];
  15. mysqli_free_result($recordSet);
  16. // If the tag already exists in the database
  17. if($tagCheck == $tag) {
  18. // Update description
  19. $query = 'UPDATE tags SET description="' . $description . '" WHERE tag="' . $tag . '" AND canvas_id="' . $canvasId . '";';
  20. }
  21. // Else if the tag is new
  22. else {
  23. // Add new tag with the description
  24. $query = 'INSERT INTO tags (tag, description, canvas_id) VALUES ("' . $tag . '", "' . $description . '", "' . $canvasId . '");';
  25. }
  26. // Insert new or update old description
  27. db_query_no_return($database, $query);
  28. // Close the connection to the database
  29. db_close($database);
  30. }
  31. catch(Exception $e) {
  32. $errorMsg = $e->getMessage();
  33. print_r($errorMsg);
  34. }
  35. ?>