doupai6875 2017-04-05 14:57
浏览 48

提交表单onchange复选框并更新mysql db

<?php
if(isset($_POST['chkStatus'])){
    for ($i=0;$i<count($_POST['chkStatus']);$i++) {
        $count = count($_POST['chkStatus']);
        list($txtClientId, $txStatusValue) = explode('-', $_POST['chkStatus'][$i], 2);
        $stmtChkStatus=$con->query("SELECT id,status FROM users WHERE id=$txtClientId");
        $SRow = $stmtChkStatus->fetch();
        if($SRow['status']!=$txStatusValue) {
            if($txStatusValue==0) $stmtStatus=$con->query("UPDATE users SET status='0' WHERE id=$txtClientId");
            else $stmtStatus=$con->query("UPDATE users SET status='1' WHERE id=$txtClientId");
        }
    }
}
?>

<html>
    <body>
        <dl>
            <?php $stmtUsers=$con->query("SELECT id,status FROM users");
            while($UsersRow = $stmtUsers->fetch()){
                echo "<dt>$UsersRow[name]</dt>
                <dd><input type='checkbox' name='chkStatus[]' value='$UsersRow[id]-$UsersRow[status]'";
                if($CRow['status']==0) echo " checked";
                echo " onchange='this.form.submit()'>
                </dd>";
            ?>
        </dl>
    </body>
</html>

The issue with my code is that it only execute the checked checkboxes. I need a code that changes the status in SQL DB of the checkbox that i click(onchange).

  • 写回答

2条回答 默认 最新

  • douchensou6495 2017-04-05 15:00
    关注

    Instead of looping through $_POST['chkStatus'] you could fetch all possible values with $con->query("SELECT id,status FROM users") and loop over those values. You can check then, if $_POST['chkStatus'] is set for the according value(s).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog