Jump to content

Adding more than one value to a db table field


homchz

Recommended Posts

I would like to have a list of check boxes and have each value entered into a db table feild seperated by a comma for separation later.

I have tried having a group of check boxes with the same name posted into the db but it only grabs the last one.

<input name='category' type='checkbox' dir='ltr' value='9,' /.>
<input name='category' type='checkbox' dir='ltr' value='10,' />
<input name='category' type='checkbox' dir='ltr' value='11,' />
<input name='category' type='checkbox' dir='ltr' value='12,' />
<input name='category' type='checkbox' dir='ltr' value='13,' />

I want to $_POST['category'] and if all are checked I would get

9,10,11,12,13,

Is this even possible?

Thanks,

Josh
Try this: [code]<input name='category[]' type='checkbox' dir='ltr' value='9,' /.>
<input name='category[]' type='checkbox' dir='ltr' value='10,' />
<input name='category[]' type='checkbox' dir='ltr' value='11,' />
<input name='category[]' type='checkbox' dir='ltr' value='12,' />
<input name='category[]' type='checkbox' dir='ltr' value='13,' />[/code]
You can then try this on your processing page to see how its structured:
[code]<?php
echo "<pre>";
print_r($_POST['category']);
echo "</pre>";
?>[/code]
That works great for getting the values together, thanks.

However it inserts "Array" into the db table and not the numbers.

could I then insert the numbers as

$categery[$i] after a count and a for stamement or am I over thinking this??
[code]$db_cats = join (',' , $_POST['category'];[/code]

A better table design would be to to use a separate table to hold categories. so if item #1 is in categories 1,5,7 and item #2 is in cats 5,6 you'd have

[code]itemid  |  catid

   1    |    1
   1    |    5
   1    |    7
   2    |    5
   2    |    6[/code]

Now it's easy to query which cats an item belongs to and also which items are in a particular category

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.