Assuming a schema like this:
- Post (id, author_id, post_date, post_text);
- Tag (id, tag_name);
- Post Tag (id, post_id, tag_id).
The Post Tag table is what's called a join table for the many-to-many relationship from Post to Tag (because a Post can have multiple Tags and a Tag can be used on multiple Posts).
The user enters a list of tags separated by commas into an input field:
$tags = array_unique(array_map(explode(',', $POST['tags']), 'trim'));
and you have an array of tags the user entered. You may want to sanitize them further, like only allowing certain characters and/or converting them to lowercase.
Then your code becomes:
$post_id = ...
foreach ($tats as $tag) {
$tag = mysql_real_escape_string($tag);
$sql = <<<END
INSERT INTO PostTag (post_id, tag_id)
VALUES ($post_id, (SELECT id FROM Tag WHERE tag_name = '$tag'))
END;
mysql_query($sql);
}