homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

checkbox insert - php/mysql
Multiple fields, not multiple records....

 1:23 am on Dec 6, 2006 (gmt 0)


If anyone has any ideas on this I'd really appreciate any feedback...

I've a database where I'd like users to be able to insert new products. I'm presenting them with an insertion form that, amongst other things, offers a checkbox where they can select 5 attributes to describe their product (from about 20 different options).

When they hit 'submit' the entry is added to the db and a unique ID is given to the product. What I'm stuck on is how can I list each of the 5 selected attributes from the checkbox (no matter which ones were selected) into respective field columns of 'attribute1', 'attribute2', 'attribute3', attribute4' and 'attribute5'. (I'm using those field titles to pull recordsets in an advanced search function, hence the need to keep it like that).

Many thanks,



smells so good

 2:46 am on Dec 6, 2006 (gmt 0)

You'll need to give a unique name value to each of the options. That's easy enough to generate when you build the page. One option that I'm thinking will work is to have your form post back to the page, and then you retrieve the checked attributes from the POST array, then build your query and write to the database. Immediately follow that with a header call and you're right back to the beginning of the page.

Do you care if they choose more than 5 attributes? Someone will come along and select all of them you know.. You can fill your columns from the POST array until you've reached 5 elements, disregarding the rest. That would be the easiest method. Depending on their order in the POST array you may be taking some of their 'less important' choices if they select more than five. You can prevent them from ever selecting more than 5 attributes with the use of JavaScript on your page. That get's nasty and I'll wish you lots of luck if you want to go that route :) Actually, it's not so hard that I haven't done that very thing, but it been a while. What I did was to allow them to make as many selections as were available, but made sure that they know they COULD ONLY SELECT 3. Then, on the post, I have an on-click event calling a JS function that tallies up the number of selected boxes and returns it to my form. Too many, or too few, and they get to try again.


 9:21 pm on Dec 6, 2006 (gmt 0)

Thanks for the response, Smells. While you completely lost me with your opening paragraph, you've certainly added more things for me to consider - i.e. level of importance to each attribute and limiting the number to just 5. As much of a headache as this is going to be, it's worth setting it up right in the first place, so thanks for the ideas.


 9:51 pm on Dec 6, 2006 (gmt 0)

Here's something I wrote up real quick; I hope there are no errors ;)

<form method="post" action="index.php">
Selection one<input type="checkbox" name="selection[b][][/b]" value="1"><br/>
Selection two<input type="checkbox" name="selection[b][][/b]" value="2"><br/>
Selection three<input type="checkbox" name="selection[b][][/b]" value="3"><br/>
Selection four<input type="checkbox" name="selection[b][][/b]" value="4"><br/>
Selection five<input type="checkbox" name="selection[b][][/b]" value="5"><br/>
Selection six<input type="checkbox" name="selection[b][][/b]" value="6"><br/>
Selection seven<input type="checkbox" name="selection[b][][/b]" value="7"><br/>
<input type="submit" value="monkey!" />
if(![url=http://us3.php.net/manual/en/function.isset.php]isset[/url]($_POST)) [url=http://us3.php.net/manual/en/function.exit.php]exit[/url]();
$selections = (isset($_POST['selection']))?$_POST['selection']:NULL;
$count = [url=http://us3.php.net/manual/en/function.count.php]count[/url]($selections);
if($count > 5 $count < 1) { //checks to see if they selected more than 5
echo 'Danger Will Robinson!';
$query = 'INSERT INTO table (attribute1,attribute2,attribute3,attribute4,attribute5) VALUES (';
$add = "'".[url=http://us3.php.net/manual/en/function.implode.php]implode[/url]("','",$selections)."'";
for($i = 0; $i < 5-$count; $i++) {
$add .= ",NULL";
$query .= $add.')';
echo $query;

There are other ways of doing this, too.

Best of luck! :)

P.S. Remember to replace the with the solid representation! WW breaks these

P.P.S 1000 post club! woohoo!


 10:50 pm on Dec 6, 2006 (gmt 0)

eelixduppy, many thanks for taking the time to do that - it's very much appreciated (and congrats on the 1000th post!).

I can get the output to echo off fine, and all things look to be in the right place (and the limit function works a treat), but it just doesn't want to insert into my db for some reason. I suspect it's something simple I'm overlooking at my end so I think I'll take a break and get back to it in a while.

Again, many thanks for your time.


 10:58 pm on Dec 6, 2006 (gmt 0)

Try using mysql_error [us3.php.net]


mysql_query($query) or die(mysql_error());

Also check your PHP error logs.

BTW, thanks :)


 11:11 pm on Dec 6, 2006 (gmt 0)

All I can really say is 'WooHoo!'

Champion mate, much appreciated.


 12:59 am on Dec 7, 2006 (gmt 0)

One fatal thing I forgot to handle, was escaping the query variables. I am ashamed :/

I think the best solution would be to use array_map [us2.php.net].


$query = 'INSERT INTO table (attribute1,attribute2,attribute3,attribute4,attribute5) VALUES (';

[b]$selections = array_map("[url=http://us3.php.net/manual/en/function.mysql-real-escape-string.php]mysql_real_escape_string[/url]",$selections);

$add = "'".implode("','",$selections)."'";

Don't forget to escape your other variables as well, if any.

Sorry about that. I guess I have too many things on my mind :)


 2:07 am on Dec 8, 2006 (gmt 0)

Ahh, thanks for the heads up. Traps for young players! My default input form (the one DW creates automagically and the one I'm making a career of trying to customise) inserts 'mysql_escape_string' for each of the variables, but I was too lazy/ignorant to wonder why. Now I know :) The last hour or so reading up on the topic was well worth it.

Again, your input and time is very much appreciated.


 3:43 am on Dec 8, 2006 (gmt 0)

Thanks for this, eelixduppy:

$selections = array_map("mysql_real_escape_string",$selections);

That little snippet will help delay my inevitable carpal tunnel by at least a year. :D

Congrats on hitting 1,000 posts. :)


 4:10 am on Dec 8, 2006 (gmt 0)

That little snippet will help delay my inevitable carpal tunnel by at least a year. :D

haha...I wasn't sure that it was the best solution, but then I experimented with it and it looks good and works.

And thanks for the congrats :)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved