dpwgzi7987 2018-03-28 07:45
浏览 46
已采纳

php mysql复选框在循环中保存

I have this loop for list rows, what I want to do is return to base 1 or 0 (1 check, 0 uncheck) to that row that is listed. I manage to do this without the WHILE function, but in while function it won't work.

<?php

    $interval = $conn->query("SELECT ID,Vrsta_segmenta, Active FROM `msa_segmenti`");
    if ($interval->num_rows > 0) {
        while($row = $interval->fetch_array()) {
            $checked =  $row["Active"]; 
    ?>                              
    <form name="update" method="POST" action="msa_pauze_admin.php" class="form-horizontal form-label-left">

        <div class="form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12"><?php echo $row["Vrsta_segmenta"];?></label>
            <div class="col-md-9 col-sm-9 col-xs-12">
                <div class="">
                    <label>
                        <input type="checkbox" name="check" value="<?php echo $row["Active"];?>" class="js-switch" 
                        <?php
                        if ($checked == '1') { ?> checked
                        <?php
                        } else if ($checked == '0')  "" 
                        ?>
                    </label>
                </div>
            </div>
        </div>
<?php  }} ?>              
<button type="submit" class="btn btn-success" value="Save">Save</button>

Now i need to return value to the db when i click save button. I know i need to check click state so i try this but did not work:

if (isset($_POST["check"])) {
    $check_value == '1';
    $interval_check_save = "UPDATE `msa_segmenti` SET `Active` = '".$check_value."' WHERE `ID` = '".$id."'";
    $mysqli->query($interval_check_save);
} else {
    $check_value == '0';
    $interval_check_save = "UPDATE `msa_segmenti` SET `Active` = '".$check_value."' WHERE `ID` = '".$id."'";
    $mysqli->query($interval_check_save);

}
  • 写回答

4条回答 默认 最新

  • dongtao1262 2018-04-04 09:39
    关注

    I manage to solve my problem:

    <form name="update" method="POST" action="msa_pauze_admin.php" class="form-horizontal form-label-left">
    <?php
                                        if (!empty($_POST['Active'])) {
                                            $interval_check = "UPDATE `msa_segmenti` SET `Active` = 0 ";
                                            $mysqli->query($interval_check);
                                        }
                                        $interval = $conn->query("SELECT ID,Vrsta_segmenta, Active FROM `msa_segmenti`");
                                        if ($interval->num_rows > 0) {
                                        while ($row = $interval->fetch_array()) {
                                            $id = $row["ID"];
                                            if (isset($_POST['Active'][$id])) {
                                                $interval_check = "UPDATE `msa_segmenti` SET `Active` = 1 WHERE `ID` = '" . $_POST['Active'][$id] . "'";
                                                $mysqli->query($interval_check);
    
                                            }
                                        }
    
                                            $interval = $conn->query("SELECT ID,Vrsta_segmenta, Active FROM `msa_segmenti`");
                                            while ($row = $interval->fetch_array()) {
    //print_r($row);
                                                $checked = $row["Active"];
                                                $id = $row["ID"];
                                                //echo count($_POST['Active']);
                                                //echo "<pre>"; print_r($_POST); exit;
    
                                                ?>
    
            <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12"><?php  echo $row["Vrsta_segmenta"]; ?></label>
                            <div class="col-md-9 col-sm-9 col-xs-12">
                                                        <div class="">
                                                            <label>
                                                                <input type="checkbox" name="Active[<?php echo $id; ?>]"
                                                                       value="<?php echo $id; ?>"
                                                                       class="js-switch" <?php echo ($checked == 1) ? 'checked' : ''; ?>>
                                                            </label>
                                                        </div>
                                                    </div>
                    </div>
    
    
    
    <?php
    
        }
    } 
    
    ?>                              
                        <button type="submit" class="btn btn-success" value="Save">Save</button>
                        </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器