<?php
    require_once("../../php/db_utils.php");
    
    try {
        // Connect to the database
        $database = db_connect();
        
        // Incoming data
        $newTag = $_POST["tag"];
        $canvasId = $_POST["canvas_id"];
        
        // Declare query
        $query = "SELECT tag FROM tags WHERE tag = '" . $newTag . "' AND canvas_id = '" . $canvasId . "';";
        
        // Run query
        $recordSet = db_query_return($database, $query);
        
        // Get the tag if it exists, and return an empty string if it does not exist
        $record = mysqli_fetch_assoc($recordSet);
        
        $tag = $record["tag"];
        
        mysqli_free_result($recordSet);
        
        echo($tag);
        
        // Close the connection to the database
        db_close($database);
    }
    catch(Exception $e) {
        $errorMsg = $e->getMessage();
        print_r($errorMsg);
    }
?>