rachae1 Posted September 13, 2008 Share Posted September 13, 2008 Hi I am trying to add comma seperating categories into the database by a form and then exploding them before they are submitted but I am doing the code incorrently and I cant figure it out. At the moment it just adds the word 'array' to the database. Can someone tell me how I do it please. Thanks in advance. if ($submit == 'Addtag') { $tags = "tags"; $tags = explode(",",$_POST['tags']); //begin error reporting $error_msg = array(); if(empty($tags)) { $error_msg[] = "No tags entered!<br />"; } //print errors, if any if(count($error_msg)>0) { echo "<strong>ERROR:</strong><br>n"; foreach($error_msg as $err) echo "$err"; } //else, no errors, insert to the DB! else { $query = mysql_query("INSERT INTO category_tags (tags) VALUES ('$tags')") or die(mysql_error()); echo "Tags Added!"; } } Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/ Share on other sites More sharing options...
DarkWater Posted September 13, 2008 Share Posted September 13, 2008 You can't echo an array or use it as a string. Think about it. Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640629 Share on other sites More sharing options...
rachae1 Posted September 13, 2008 Author Share Posted September 13, 2008 Hi Thanks for your reply. Sorry I am new to arrays but trying to learn them. Can I not add comma seperated keywords and seperate it before it goes into the database ? Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640631 Share on other sites More sharing options...
Stooney Posted September 13, 2008 Share Posted September 13, 2008 Do you want a separate row per keyword in the database? Or do you want all the keywords entered into a single record? Show us what you expect it to look like. Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640637 Share on other sites More sharing options...
rachae1 Posted September 13, 2008 Author Share Posted September 13, 2008 Hi I have just changed my code taken from a tutorial it now looks like this : if ($submit == 'Addtag') { $taginput = $_POST["tags"]; $tagarray = explode(",",$taginput); for($i=0;$i<count($tagarray);$i++){ $tags = mysql_real_escape_string(stripslashes(ltrim(rtrim($tagarray[$i])))); if($tags == "") continue; $query = "INSERT INTO category_tags (tags) VALUES ($tags')"; mysql_query($query); } 23 Array 24 Array 25 Array } I entered 3 keywords but each of those keywords now says Array instead of the keyword name. My database looks like this Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640642 Share on other sites More sharing options...
rachae1 Posted September 13, 2008 Author Share Posted September 13, 2008 eugh sorry if ($submit == 'Addtag') { $taginput = $_POST["tags"]; $tagarray = explode(",",$taginput); for($i=0;$i<count($tagarray);$i++){ $tags = mysql_real_escape_string(stripslashes(ltrim(rtrim($tagarray[$i])))); if($tags == "") continue; $query = "INSERT INTO category_tags (tags) VALUES ($tags')"; mysql_query($query); } Database 23 Array 24 Array 25 Array } Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640643 Share on other sites More sharing options...
rachae1 Posted September 13, 2008 Author Share Posted September 13, 2008 infact that code dont work at all for me. used it from http://www.brainhandles.com/2007/04/26/simple-tagging-with-php-mysql/ :S Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640649 Share on other sites More sharing options...
Stooney Posted September 13, 2008 Share Posted September 13, 2008 Give this a try: <?php if($submit == 'Addtag') { $taginput = $_POST["tags"]; $tagarray = explode(",",$taginput); $count=count($tagarray); for($i=0; $i<$count; $i++){ $tags = mysql_real_escape_string(stripslashes(ltrim(rtrim($tagarray[$i])))); if(strlen($tags)>0){ $query = "INSERT INTO category_tags (tags) VALUES ('$tags')"; mysql_query($query) or die(mysql_error()); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640664 Share on other sites More sharing options...
rachae1 Posted September 13, 2008 Author Share Posted September 13, 2008 Thank you that worked a treat Quote Link to comment https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.