herghost Posted April 22, 2011 Share Posted April 22, 2011 Hi Guys Whats the easiest way to store tags in a database from a php form? If I have a text field called tags, and each tag is comma seperated how do I process the data? I have a table called tags, with 3 fields, tag_id, post_id and tag Basically how would I basically turn this input: a tag, b tag, c tag from a form field to tag_id cat_id tag 1 dont worry about me, i can work this out! tag a 2 dont worry about me, i can work this out! tag b 3 dont worry about me, i can work this out! tag c Many Thanks Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/ Share on other sites More sharing options...
fugix Posted April 22, 2011 Share Posted April 22, 2011 mysql_query("insert into tags values('tag_id', 'post_id', 'tag')"); Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204882 Share on other sites More sharing options...
wildteen88 Posted April 22, 2011 Share Posted April 22, 2011 If I have a text field called tags, and each tag is comma seperated how do I process the data? Use explode. Example $tags = explode(',', $_POST['your_tag_field']; print_r($tags); To insert the tags into your tags table I'd do something like this $cat_id = (int) $cat_id; $query = 'INSERT INTO tags (cat_id, tag) VALUES ('; // dynamically build the query foreach($tags as $tag) { $tag = mysql_real_escape_string($tag); $query .= "($cat_id, '$tag'), "; } // clean the query up, remove the comma and space at the end of the query string $query = substr($query, 0, -2); mysql_query($query); Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204892 Share on other sites More sharing options...
herghost Posted April 22, 2011 Author Share Posted April 22, 2011 Thanks, I now have this: $post_id = '1'; $tags = explode(',', $_POST['tags']); print_r($tags); $query = 'INSERT INTO blog_tags (post_id, tag) VALUES ('; // dynamically build the query foreach($tags as $tag) { $tag = mysql_real_escape_string($tag); $query .= "('$post_id', '$tag'), "; } // clean the query up, remove the comma and space at the end of the query string $query = substr($query, 0, -2); mysql_query($query); if(!$query) { echo mysql_error(); } else { echo "ok"; } However nothing is entered into the db, the query prints the OK echo, and if I remove the else the printf works as well? Cheers Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204902 Share on other sites More sharing options...
fugix Posted April 22, 2011 Share Posted April 22, 2011 if you have 3 fields in your database then you will need to insert 3 pieces of data..his code only inserts 2 so it wont work Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204904 Share on other sites More sharing options...
herghost Posted April 22, 2011 Author Share Posted April 22, 2011 fujix: tag_id is ai, so not needed Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204905 Share on other sites More sharing options...
fugix Posted April 22, 2011 Share Posted April 22, 2011 You still Need empty quotes fir the field Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204908 Share on other sites More sharing options...
herghost Posted April 22, 2011 Author Share Posted April 22, 2011 fugix: Added, no difference Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204910 Share on other sites More sharing options...
Pikachu2000 Posted April 22, 2011 Share Posted April 22, 2011 Have you echoed the query string, along with mysql_error() to help debug it? Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204912 Share on other sites More sharing options...
wildteen88 Posted April 22, 2011 Share Posted April 22, 2011 Small typo remove the ( from the end of this line $query = 'INSERT INTO blog_tags (post_id, tag) VALUES ('; Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204913 Share on other sites More sharing options...
fugix Posted April 22, 2011 Share Posted April 22, 2011 If you didn't have the parenthesis it wouldn't be a valid query Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204914 Share on other sites More sharing options...
herghost Posted April 22, 2011 Author Share Posted April 22, 2011 If you didn't have the parenthesis it wouldn't be a valid query Please stop answering if you dont know what you are going on about WildTeen & Pikachu: Thanks, its now working using: $post_id = '1'; $tags = explode(',', $_POST['tags']); print_r($tags); $query = 'INSERT INTO blog_tags (post_id, tags) VALUES '; // dynamically build the query foreach($tags as $tag) { $tag = mysql_real_escape_string($tag); $query .= "('$post_id', '$tag'), "; } // clean the query up, remove the comma and space at the end of the query string $query = substr($query, 0, -2); $go=mysql_query($query); if(!$go) { echo mysql_error(); } else { echo "ok"; echo '<br>'; echo $go; } Link to comment https://forums.phpfreaks.com/topic/234446-phpmysql-tags/#findComment-1204916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.