Match tag names with form titles

I don’t see PHP syntax errors (like unwanted brackets) in your code, but there are two WordPress-specific issues that need to be fixed:

  1. Note that wpdb::prepare() needs one or more placeholders (e.g. %s for strings and %d for numbers) and the replacement value for each placeholder.

    So in your case, the correct $wpdb->prepare() would be:

    $wpdb->prepare( "SELECT ID FROM wptq_forms WHERE name = %s", $tag_name );
    
  2. get_the_tags() returns an array of term objects on successful requests, and each object is a WP_Term instance which does not have a tag_name property, only name.

    Therefore $the_tag[0]->tag_name should instead be $the_tag[0]->name.