I have a PHP form with 4 text fields and 2 checkboxes. The checkboxes are “tags” that can be applied to the articles that are input using the text fields.
For example:
Information:
Title – Defining intransitive relationships of the migratory birds of South America
Author – Author. S. Wellington
Access Date – 19/06/2012
URL – http://www.wellington.org/articles/birds/def-int-rel-mig-bird-s-amer.pdf
Tags:
Birds[X] South America[X]
In this case both of the checkboxes would be checked, because the article pertains both to birds and South America.
I have a database with 3 tables in it:
articles: id – articletitle – articleorganization – articledate – articleurl
tags: id – tag_content
articles_tags: article_id – tag_id
Currently, my form can insert the article information into the articles table using:
mysql_query("INSERT INTO articles SET articletitle='$articletitle',
articleorganization='$articleorganization',
articledate='$articledate',
articleurl='$articleurl' ")
and then, I can use $article_id = mysql_insert_id(); to get the id that would be inserted into the articles_tags table.
But after that I am totally stumped.
I need to somehow loop over the checkboxes:
<input type="checkbox" name="articletags_birds" value="Birds" id="articletags_0" />
<input type="checkbox" name="articletags_southamerica" value="South America" id="articletags_1" />
but I have no idea how I should be doing that.
Do the same for the other tags. If you have a very large number (5+), or want users to input their own tags, just do something like:
Note:
1.) There will be no POST sent if there is no checkbox selected!
2.) strpos will check if the tag starts with articletags_
3.) please ESCAPE the the queries! I’ve just shorthanded it all, but malicious PHP can be injected here
Comment if I’m unclear / if its still not working