复选框随机检出 [英] Checkboxes are checking out randomly

查看:192
本文介绍了复选框随机检出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。



我有一个含有数百个生成的数百个复选框的页面。所以当数据库返回1时,它被检查,否则它为0,不检查。当我尝试检查复选框,并尝试更新到数据库,一些复选框是随机检出..



代码:



这是查询

  if(isset($ _ POST ['submit'])){
foreach($ _POST ['untrain [{$ room-> room_id}]'] as $ room_id){
//这个查询需要保护SQL注入!
$ user_id;
$ room_id;
$ untrainQuery =UPDATE room_users SET trained ='1'WHERE room_id = $ room_id;
$ db-> update($ untrainQuery);

}

}

复选框:

 <?php 
if($ room-> trained == 1){
> < input type ='checkbox'value =<??php
echo $ room-> user_id;
?> name =trained [<??php
echo $ room-> room_id;
?>] <?php
echoY;
} else if($ room-> trained == 0){
?> < input type ='checkbox'value =<??php
echo $ room-> user_id;
?> name =untrain [<??php
echo $ room-> room_id;
?>]> <?php
echoN;
}
?> < / td>
< Td><?php
if($ room-> active == 1){
? < input type ='checkbox'name =<?php
echo $ room-> room_id;
?>检查> <?php
echoActive;
} else {
?> < input type ='checkbox'name =<?php
echo $ room-> room_id;
?> <?php
echoInactive;
}
?>

所以当数据库返回1的复选框被选中,否则它是0所以未选中..所以我的问题

我想选中复选框以将数据库更新为1,但有时它会随机选中复选框?!

解决方案

您的逻辑有以下缺点:您的 $ _ POST 数组有键 untrain ,其值是一个内部数组键 room_id (因为它们在复选框名称中)和值 user_id (复选框的值)。在你的foreach循环 $ room_id 已经批准复选框的值,这确实 user_ids 。除了你应该迭代 $ _ POST ['untrain'] ,我不知道你把这个键 $ room-> room_id 从。



我会将其更改为:

  if(isset($ _ POST ['submit'])){
foreach($ _POST ['untrain'] as $ room_id => $ user_id){
// sanitize $ room_id
$ untrainQuery =UPDATE room_users SET trained ='1'WHERE room_id = $ room_id;
$ db-> update($ untrainQuery);
}
}

或者,如果你有一个所有room_ids的数组,你可以迭代它们来检查是否检查:

  if(isset($ _ POST ['submit' 
foreach($ room_ids as $ room_id){
// sanitize $ room_id
if(isset($ _ POST [untrain [{$ room_id}]]){//也就是说,如果检查
$ trained = 1;
} else {
$ trained = 0;
}
$ untrainQuery =UPDATE room_users SET trained = $ trained WHERE room_id = $ room_id;
$ db-> update($ untrainQuery);
}
}


I've got a question.

I've got a page with hundreds of checkboxes wich are generated with the Database. So when the database returns 1 it is checked otherwise it is 0 and not checked. When I try to check the checkbox and try to update that into the database, some checkboxes are randomly checking out..

Code:

This is the Query

if(isset($_POST['submit'])){
                foreach ($_POST['untrain[{$room->room_id}]'] as $room_id) {
                    // This query needs protection from SQL Injection!
                    $user_id;
                    $room_id;
                $untrainQuery = "UPDATE room_users SET trained = '1'  WHERE room_id = $room_id";
                $db->update($untrainQuery);

                }

                }

These are the checkboxes:

<?php
if ($room->trained == 1) {
?> <input type='checkbox' value="<?php
    echo $room->user_id;
?>" name="trained[<?php
    echo $room->room_id;
?>]" checked> <?php
    echo "Y";
} else if ($room->trained == 0) {
?> <input type='checkbox' value="<?php
    echo $room->user_id;
?>" name="untrain[<?php
    echo $room->room_id;
?>]"> <?php
    echo "N";
}
?> </td>
                    <Td><?php
if ($room->active == 1) {
?> <input type='checkbox' name="<?php
    echo $room->room_id;
?>" checked> <?php
    echo "Active";
} else {
?> <input type='checkbox' name="<?php
    echo $room->room_id;
?>"  <?php
    echo "Inactive";
}
?>

So when the database returns 1 the checkbox is checked otherwise it is 0 so unchecked.. So my question is why it is randomly checking out checkboxes?

I want to check the checkboxes to update the database with 1, but sometimes it randomly checking out checkboxes?!

解决方案

Your logic has the following flaws: your $_POST array has the key untrain and its value is an internal array of keys room_id (because they are in the checkbox name) and values user_id (checkbox's values). In your foreach loop $room_id has assingned the values of checkboxes, which are indeed user_ids. Besides you should iterate over $_POST['untrain'], I don't know where you take that key $room->room_id from.

I would change that to:

if(isset($_POST['submit'])){
    foreach ($_POST['untrain'] as $room_id => $user_id) {
        //sanitize $room_id
        $untrainQuery = "UPDATE room_users SET trained = '1'  WHERE room_id = $room_id";
        $db->update($untrainQuery);
    }
}

Or, if you have an array of all room_ids, you can iterate over them to check if which are checked:

if(isset($_POST['submit'])){
    foreach ($room_ids as $room_id) {
        //sanitize $room_id
        if(isset($_POST["untrain[{$room_id}]"]){//that is, if it was checked
            $trained = 1;
        }else{
            $trained = 0;
        }
        $untrainQuery = "UPDATE room_users SET trained = $trained  WHERE room_id = $room_id";
        $db->update($untrainQuery);
    }
}

这篇关于复选框随机检出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆