Forum Moderators: coopster
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,
2odd...
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.
<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!" />
</form>
<?php
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!';
exit();
}
$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!
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.
Example:
mysql_query($query) or die(mysql_error());
Also check your PHP error logs.
BTW, thanks :)
I think the best solution would be to use array_map [us2.php.net].
Example:
...
$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);
[/b]
$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 :)
Again, your input and time is very much appreciated.
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 :)