简体   繁体   中英

sql get SET values from form checkbox

I'm working on an SQL database and would like to know how to get the values of multiple checkboxes and update a SET in the database using PHP? I need to be able to select one or more values and update the set.

For example:

SQL

UPDATE options 
SET option1='$_POST[option1]', option2='$_POST[option2]', option3='$_POST[option3]',
WHERE... ";

PHP

echo "<input type=checkbox name=option1 value=" " /> ";
echo "<input type=checkbox name=option2 value=" " /> ";
echo "<input type=checkbox name=option2 value=" " /> ";
UPDATE options 
SET option1='$_POST[option][0]', option2='$_POST[option][1]', option3='$_POST[option][2]',
WHERE... ";

echo '<input type=checkbox name="option[]" value=" " />';
echo '<input type=checkbox name="option[]" value=" " /> ';
echo '<input type=checkbox name="option[]" value=" " /> ';

You can get Selected Checkbox value like this,

echo '<input type=checkbox name="option[]" value="1" />';
echo '<input type=checkbox name="option[]" value="2" /> ';
echo '<input type=checkbox name="option[]" value="3" /> ';

Then You can get that values & set it in a serialize format to insert in database like this,

$values = $_POST['option'];
$values = serialize($values); 

Here You can insert $values in database field. In a same way, while updating you can fetch field value & unserialize it. Then compare it to the original value. if it is in the array then you can checked it.

$options = $_GET['option'];

foreach ($options as $option){
    echo $option
}

try like this

$i=0;
while($i<3)
{
if(!isset($_POST['option'][$i]))
{
$_POST['option'][$i]="";
}
$i++;
}
UPDATE options SET option1='$_POST[option][0]', option2='$_POST[option][1]', option3='$_POST[option][2]',
WHERE... ";


echo '<input type=checkbox name="option[0]" value=" " />';
echo '<input type=checkbox name="option[1]" value=" " /> ';
echo '<input type=checkbox name="option[2]" value=" " /> ';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM