how to update checked value and unchecked value in database using php?

时间:2015-11-12 11:12:21

标签: php mysql

I have multiple checkboxes. once the user checked i need to store these values into db. how to store it in database according to checked values . I have tried with the following code but it is not working. please advise on this

 <input name="weekdays[]" val="mon">
<input name="weekdays[]" val="tue">
<input name="weekdays[]" val="wed">
<input name="weekdays[]" val="fri">
<input name="weekdays[]" val="sat">
$get_array =  $_POST['weekdays'];
foreach($get_array as $value){
//   if monday not having value set zero in column
// update into multiple column
//update table set is_monday_available=$mon,is_tue_availble=$tues,etc where id= busi_id
}

Solution:

  foreach ($get_checkbox as $key => $check) {

            if(in_array("mon",$get_checkbox)){
                $values['mon'] = 1;
            }else{
                $values['mon'] = 0;
            }
            if(in_array("tue",$get_checkbox)){
                $values['tue'] = 1;
            }else{
                $values['tue'] = 0;
            }
            if(in_array("wed",$get_checkbox)){
                $values['wed'] = 1;
            }else{
                $values['wed'] = 0;
            }
            if(in_array("thu",$get_checkbox)){
                $values['thu']= 1;
            }else{
                $values['thu']= 0;
            }
            if(in_array("fri",$get_checkbox)){
                $values['fri'] = 1;
            }else{
                $values['fri'] = 0;
            }
            if(in_array("sat",$get_checkbox)){
                $values['sat'] = 1;
            }else{
                $values['sat'] = 0;
            }
            if(in_array("sun",$get_checkbox)){
                $values['sun'] = 1;
            }else{
                $values['sun'] = 0;
            }

    }

     $update_data = array("is_monday_available" => $values['mon'],
                            "is_tuesday_available" => $values['tue'],
                            "is_wednesday_available" =>$values['wed'],
                            "is_thursday_available" => $values['thu'],
                            "is_friday_available" => $values['fri'],
                            "is_saturday_available" => $values['sat'],
                            "is_sunday_available" => $values['sun']                             
                            );                          

1 个答案:

答案 0 :(得分:3)

To print the checkboxes you can use:

$query = "SELECT is_mon_available, is_tue_available FROM myTable";
$stmt = $pdo->query($query);
$row = $stmt->fetch($stmt, PDO::FETCH_ASSOC);

$weekdays = array('mon', 'tue', 'wed', 'fri', 'sat');
foreach ($weekdays as $weekday) {
    echo '<input type="checkbox" name="weekdays[' . $weekday . ']"';
    if ($row['is_' . $weekday . '_available']) {
        echo ' checked="checked"';
    } 
    echo ' />';
}

To store them into the database you can use:

// update the query to fill in all the weekdays and add the WHERE statement
$query = "UPDATE myTable SET is_mon_available = :mon, is_tue_available = :tue";
$stmt = $pdo->prepare($query);

$postedWeekdays = $_POST['weekdays'];

$weekdays = array_reduce(array_keys($postedWeekdays), function ($result, $key) use ($postedWeekdays) {
    $result[$key] = $postedWeekdays[$key] == 'on' ? 1 : 0;
    return $result;
}, array());

$stmt->bindValues($weekdays);
$stmt->execute();

I've used PDO for database provisioning.