Forum Moderators: coopster

Message Too Old, No Replies

insert more than one selection (checkboxes) into the database

         

someone

4:46 pm on May 11, 2005 (gmt 0)

10+ Year Member



Hi guys,

I have a couple of checkboxes and users can select all the apply. I am trying to think what the best way is to insert the data into the database. Should I give each checkbox a different name (vote1, vote2, vote3) and have three columns in the database table for each of them?

I need to use the data entered into the database to count up how many people selected apple, how many selected banana, and how many selected orange. Any suggestions would have appreciated.

<input type="checkbox" name="vote1" value="apple">Apple

<input type="checkbox" name="vote2" value="banana">Banana

<input type="checkbox" name="vote3" value="orange">Orange

<INPUT name="submit" type="submit" value="submit">

ergophobe

6:17 pm on May 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<input type="checkbox" name="vote[]" value="apple">Apple

<input type="checkbox" name="vote[]" value="banana">Banana

Then...


foreach($_POST['vote'] as $fruit)
{
switch
{
case 'apple':
$apple++;
break;

case 'banana':
$banana++;
break;
}
}

Blackie

6:44 am on May 12, 2005 (gmt 0)

10+ Year Member



I would create a "votes" table like this

id user_id vote
1 5 1
2 5 3
3 7 1
4 9 1
5 9 2

then you can SELECT you to death counting choices here :-)