动态输入名称php

时间:2015-03-31 10:23:36

标签: php mysql pdo mysqli

我有几个带有动态名称的复选框,它来自数据库。我把复选框放在一个表单中,我试图将它们的值更新到数据库中。

以下是它的样子:

if(isset($_POST['submit'])) 
                    {
                        $db->exec("UPDATE room_users SET trained = 1 WHERE user_id = $userId");
                    }

                ?>
                <form class='verwerkInfo' method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>?license=6'>
                <td>
                    <?php if($room->trained == 1) { ?> <input checked type="checkbox" name="<?php echo $room->user_id ?>"> <?php echo "Y";} else { ?> <input type="checkbox" name="<?php echo $room->trained; ?>"> <?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";} ?>
                </td>

                <Td><?php echo $room->configuration; ?></td>
                <td><?php echo $room->room_id; ?></td>
                </tr>

            <?php
            $totalSessions += $sessions->total;
            $count++;
        }

        $javaScriptArray .= "]";


        ?>
        <tr>

            <th>Mteeting rooms: <?php echo $count; ?></h>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
            <th><?php echo $totalSessions; ?></th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
                            <Th><input type='submit' id='verwerken' value='update de gegevens' name='submit'></th>
                </form>

我想更新此处的第一个复选框,因此在提交时,数据库中的值将转为1

这是我正在使用的查询:

$db->exec("UPDATE room_users SET trained = 1 WHERE user_id = $userId");

<?php
if($room->trained == 1) {
?>
<input checked type="checkbox" name="<?php echo $room->user_id ?>">
<?php echo "Y";} else { ?>
<input type="checkbox" name="<?php echo $room->trained; ?>">
<?php echo "N";} ?></td>

1 个答案:

答案 0 :(得分:0)

"UPDATE `room_users` SET `trained` ='1' WHERE `user_id` = '$userId'"

更改您的查询然后尝试此操作。