Forum Moderators: coopster

Message Too Old, No Replies

Inserting multiple values in db row using checkboxes.

         

dkin

11:56 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



I am trying to insert multiple text values into a database row using checkboxes. This is what I have.

Checkboxes for each class

Pal
Sk
Bar
Gno

Person checks all that apply and I would like the values of all inserted into a row in the db.

This is my form.


<tr>
<td>War</td>
<td><input type=checkbox name='Classes[0]' value='War'></td>
<td>Mnk</td>
<td><input type=checkbox name='Classes[1]' value='Mnk'></td>
<td>Drd</td>
<td><input type=checkbox name='Classes[2]' value='Drd'></td>
<td>Mag</td>
<td><input type=checkbox name='Classes[3]' value='Mag'></td>
<td>Pal</td>
<td><input type=checkbox name='Classes[4]' value='Pal'></td>
</tr>
<tr>
<td>Ran</td>
<td><input type=checkbox name='Classes[5]' value='Ran'></td>
<td>Sha</td>
<td><input type=checkbox name='Classes[6]' value='Sha'></td>
<td>Nec</td>
<td><input type=checkbox name='Classes[7]' value='Nec'></td>
<td>Sk</td>
<td><input type=checkbox name='Classes[8]' value='Sk'></td>
<td>Brd</td>
<td><input type=checkbox name='Classes[9]' value='Brd'></td>
</tr>
<tr>
<td>Cl</td>
<td><input type=checkbox name='Classes[10]' value='Cl'></td>
<td>Enc</td>
<td><input type=checkbox name='Classes[11]' value='Enc'></td>
<td>Rge</td>
<td><input type=checkbox name='Classes[12]' value='Rge'></td>
<td>Alc</td>
<td><input type=checkbox name='Classes[13]' value='Alc'></td>
<td>Wiz</td>
<td><input type=checkbox name='Classes[14]' value='Wiz'></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>All Classes</td>
<td><input type=checkbox name='Classes[15]' value='All'></td>
<td>No Classes</td>
<td><input type=checkbox name='Classes[16]' value='None'></td>
</tr>
</table>

When I view the db I see either the last value or "array".

Does anyone know what I should do.

dreamcatcher

5:54 am on Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Change your checkbox values to simply classes[], then use a foreach loop.

if (!empty($classes))
{

foreach ($classes as $value)
{
mysql_query("INSERT INTO table (row) VALUES ('$value')");
}

}

Hope that helps.

Netizen

8:29 am on Jun 17, 2004 (gmt 0)

10+ Year Member



I think you can leave the HTML as is and change your PHP code to something like:

$selected=implode(',',$_POST['Classes']);

which will put all the checked values into the $selected variable with commas in between. You can then put that value into a row in your db.

dkin

1:09 pm on Jun 17, 2004 (gmt 0)

10+ Year Member



That did help and it is working now but, now it prints all the values entered and is throwing off my table widths, can I insert a break or a split after every 4 values?

Also thank you for your replies I have tried 7 different forums and none produced the results this quickly, looks like I will be sticking around.

coopster

1:53 am on Jun 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, dkin!

can I insert a break or a split after every 4 values?

Sure, simply implement a loop and counter. This topic has come up quite a few times before so a quick search of this forum should produce quite a few answers for you. Here's one for starters...

Build html tables with php [webmasterworld.com]

Spontan

4:03 am on Jun 20, 2004 (gmt 0)

10+ Year Member



Actually i did that too.

Just use classes[] as ur value then when you go to store it do $classes=serialize($classes) and this somehow make it into a nice lil form then store this into your database.

When you extract it. do unserialize($classes) and its back into a nice array.

Not sure if you wanted to store the values of each check box seperately in the database or keep them in 1 field in the database.

Sterialize compacts the array into 1 value so if you pull them you pull the whole array at a time. Sorry if i mis-understood.

btw do not use the $trim function before you serialize..it wont work then