echo "<status>Existing tag added.</status><tag_id>{$dupCheck}</tag_id>"; //update status, tag_id echo "<tag_body>{$tag}</tag_body>"; //update tag_body } } else { //otherwise create new tag $newID = add_tag($postID, $tag); echo "<status>New tag added.</status>"; //update status echo "<tag_id>{$newID}</tag_id><tag_body>{$tag}</tag_body>"; //update tag_id, tag_body } } } else { $tagID = substr($tagID, 4); //get id in int format (splice pTag# -> #) if (isset($post->pTags[$tagID])) { //if tag already exists in post echo "<status>Tag already exists in post.</status>"; //update status } else { add_tag_relationship($postID, $tagID); //add relationship echo "<status>Existing tag added.</status><tag_id>{$tagID}</tag_id>"; echo "<tag_body>{$tag}</tag_body>"; //update status, tag_id, tag_body } } //close root element echo '</tag>';
function add_tag($post_id, $tag) { global $db; $tag = strtolower($tag); //make all tags lowercase //Insert tag into $db $addTag = $db->prepare('INSERT INTO tags (tag) VALUES (?)'); $addTag->bind_param('s', $tag); $addTag->execute(); $tagID = $db->insert_id; //save the newest made tags.id using ->insert_id property //Add id's to post_to_tags add_tag_relationship($post_id, $tagID); return $tagID; //returns the newly created tag id }