What I'm trying to do is loop through text input into which the user enters tags for a blog post. I want to add each tag to the database if it does not already exist.
The actual query line below works when I test in the database.
However, I think that my loop syntax is probably not quite right, because I am not adding anything to the database.
Can someone detect an error in my loop causing my add to the database to fail?
Thanks in advance for your help!
foreach ($_POST['__tags'] as $key=>$ls_value) {
$value = strtolower(mysql_real_escape_string($ls_value));
mysql_query("INSERT INTO `table` (`field`)
SELECT * FROM (SELECT '$value') as tmp
WHERE NOT EXISTS (
SELECT `field` FROM `table` WHERE `field` = '$value')
LIMIT 1") or trigger_error(mysql_error(), E_USER_ERROR);
}
source
share