I need some help with foreach/arrays - I want to add a small tagging function to a news script, but so far it's not working.
I either get the error "Warning: Invalid argument supplied for foreach() on line 54"
or it enters the data as "charts, tags, testing" into the database in only one row, instead of creating a new row for each of those tags.
I have defined a $tags = array(); at the top if my file, and I have a simple text input field for typing the tags.
This is the part of the script which posts the tags:
$tags = $_POST['tags'];
foreach($tags as $tag) {
$tag = strtolower($tag);
$tag = strip_tags($tag);
$tag = str_replace('ä', "ae", $tag);
$tag = str_replace('ö', "oe", $tag);
$tag = str_replace('ü', "ue", $tag);
$tag = str_replace('ß', "ss", $tag);
$tag = mysql_real_escape_string($tag);
$query_tag = "INSERT INTO news_tags VALUES ('','$newsID','$tag')";
$result_tag = mysql_query($query_tag);
if ($result_tag != false) {
echo "<br />Tag '".$tag."' hinzugefügt.";
}
}
Can anyone help out and explain how I get a new entry for each comma seperated tag in my input field?