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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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??
Link to comment
Share on other sites

Yeah, that should be fine, you could probably even do it with a foreach() for less typing...
[code]<?php
$str = "";
foreach($_POST['category'] as $x) {
    $str = $str.$x.",";
}
$str = substr($str,0,-1);
?>[/code]
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.