Forum Moderators: coopster

Message Too Old, No Replies

Inserting tags into a MySQL Table

Based on a large selection of checkboxs

         

itledi

3:16 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



I have a form I have users fill out on my website.

One portion of this form is the check all the tags that the submission falls in.

For example, if the user is making a post on Bob Marley, possible tags they could check would be "music", "people", "culture", "70's".

<li><input type="checkbox" name="music" id="tag-music" /><label for="tag-music">Music</label></li>
<li><input type="checkbox" name="people" id="tag-people" /><label for="tag-people">People</label></li>
<li><input type="checkbox" name="culture" id="tag-culture" /><label for="tag-culture">Culture</label></li>
<li><input type="checkbox" name="70" id="tag-70" /><label for="tag-70">70's</label></li>

What I need to do is to figure out how to look at all the checkboxes that have been checked from a large list, and to one by one insert them into my MySQL table using the post's hash and corresponding tag. I need some kind of loop, that I don't need to recode if I add more tags in the future.

mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-music"]."'");
mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-people"]."'");
mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-culture"]."'");
mysql_query("INSERT INTO tags (hash, tag) VALUES ('".md5($form["post"])."', '".$form["tag-to"]."'");

I'm at a loss as to where to start, or even if what I have is even good.

Any advice is most appreciated. Thanks.

HELLvin

3:39 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



give all the checkboxes the name for example: tags[]

then do a for loop on $_POST['tags']

[edited by: HELLvin at 3:40 pm (utc) on Jan. 21, 2008]

itledi

5:05 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Thank you for the reply.

I'm confused.

OK, I did more research on checkboxes. I know I want people to be able to select multiple, so I know checkboxes are the right form item.

Looking at this example [w3schools.com...] it looks like I would give all the checkboxes the same name, but different values.

<input type="checkbox" name="vehicle" value="Bike">
<input type="checkbox" name="vehicle" value="Car">
<input type="checkbox" name="vehicle" value="Airplane">

So I would have among all my other options:
<input type="checkbox" name="tag" value="music">
<input type="checkbox" name="tag" value="people">
<input type="checkbox" name="tag" value="culture">
<input type="checkbox" name="tag" value="70s">

Is that what you mean give them the name tag[]?

On the next page I'm having it it output all the contents submitted, using print_r($_POST). It looks like, even if I have multiple boxes selected, that only the last box in the list gets its value passed:

Array
(
[tag] => 70s
[x] => 44
[y] => 22
)

Is the html part of this script even right?

cameraman

5:51 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The [] in HELLvin's post is important; if you do this:
<input type="checkbox" name="vehicle[]" value="Bike">
<input type="checkbox" name="vehicle[]" value="Car">
<input type="checkbox" name="vehicle[]" value="Airplane">

<input type="checkbox" name="tag[]" value="music">
.
.

then you'll get arrays with checked items as elements of those arrays.

jatar_k

5:53 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not quite

in your first post the problem is you are using the id instead of the name

for your example above you need to add [] to each name like so

<input type="checkbox" name="tag[]" value="music">
<input type="checkbox" name="tag[]" value="people">
<input type="checkbox" name="tag[]" value="culture">
<input type="checkbox" name="tag[]" value="70s">

try that and it should work

itledi

9:31 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Thank you so much.

I was able to create a for loop statement with all the information provided:

for($i=0;$i<count($form["tag"]);$i++) mysql_query("INSERT INTO tags (hash, tag) VALUES ('$hash', '".$form["tag"][$i]."')");