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 = databaseConnect();
  6. // Incoming data
  7. $tag = $_POST["tag"];
  8. $description = $_POST["description"];
  9. $username = $_POST["username"];
  10. // Check if tag already exists
  11. $query = "SELECT tag FROM tags WHERE tag = '" . $tag . "' AND username = '" . $username . "';";
  12. $recordSet = databaseQueryWithReturn($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 username='" . $username . "';";
  20. }
  21. // Else if the tag is new
  22. else {
  23. // Add new tag
  24. $query = "INSERT INTO tags (tag, description, username) VALUES ('" . $tag . "', '" . $description . "', '" . $username . "');";
  25. }
  26. // Insert new or update old description
  27. databaseQueryNoReturn($database, $query);
  28. // Close the connection to the database
  29. databaseClose($database);
  30. }
  31. catch(Exception $e) {
  32. $errorMsg = $e->getMessage();
  33. print_r($errorMsg);
  34. }
  35. ?>