Tue Feb 16, 2010 12:37 pm - How To Update Multiple Rows In One Query
#3143
How To Update Multiple Rows In One Query
today i had a challenge which i had never done before.
have you seen websiste that have multiple checkboxes in their forms and select which choices you want and to ignore the rest
for example, lets say i have a form that looks like this:
please choose your favorite color:
Red
Blue
Green
Yellow
Orange
Black
as you can see from my form, you can choose which colors you like and i want to have a mysql query where only the boxes that are checked i want to update and ignore that ones that are not checked.
its easy, you have to give the name of the input to an array, for example like this:
<input type="checkbox" name="colors[]" value="1"> Red
so this will create an array and all i have to do is update the ones that have values in the array. so my array would look like if i select Red and Blue only: OUTPUT
once you have submited you can use a loop to put it all together:
PHP CODE:
the query would look like this:
$Query = "UPDATE MyColors SET colors='active' WHERE ColorChoices in ('".$Selected."');
if you want to see the complete script, download from this post in the attachments below
have you seen websiste that have multiple checkboxes in their forms and select which choices you want and to ignore the rest
for example, lets say i have a form that looks like this:
please choose your favorite color:
Red
Blue
Green
Yellow
Orange
Black
as you can see from my form, you can choose which colors you like and i want to have a mysql query where only the boxes that are checked i want to update and ignore that ones that are not checked.
its easy, you have to give the name of the input to an array, for example like this:
<input type="checkbox" name="colors[]" value="1"> Red
so this will create an array and all i have to do is update the ones that have values in the array. so my array would look like if i select Red and Blue only: OUTPUT
[colors] => Array
(
[0] => Red
[1] => Blue
)
once you have submited you can use a loop to put it all together:
PHP CODE:
for($counter =0; $counter < count($_POST['colors']); $counter++) {
if(!$Selected) $Selected = $_POST['colors'][$counter];
else $Selected .= ','.$_POST['colors'][$counter];
}
the query would look like this:
$Query = "UPDATE MyColors SET colors='active' WHERE ColorChoices in ('".$Selected."');
if you want to see the complete script, download from this post in the attachments below
Attachments
NOTE: You MUST be Logged in to be able to download this Attachment.
CheckBox_Array.zip
(1 KB) - Downloaded 1 Times
Login | Register
author: Webune Support
Safe Download: 0 Viruses Found