发布大量复选框

发布于 11-24 20:01 字数 3929 浏览 0 评论 0原文

更新2:

无论选中哪个选项,以下内容都会给我UDPATE table SET db_field1=0, dbfield2=0, dbfield3=0, dbfield4=0 WHERE 1=1

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

        $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4');

        $update = '';

        foreach($fields as $dbfield => $field) {
            if ($update) $update.= ',';

            $update.= ' '.$dbfield.'=';

            if (isset($_POST[field])) {
                $update.= 1;
            } else {
                $update.= 0;
            }
        }

        echo 'UDPATE table SET'.$update.' WHERE 1=1';

    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb1" />
            <input type="checkbox" name="cb2" />
            <!-- all the way to 50 -->
            <input type="checkbox" name="cb3" />
            <input type="checkbox" name="cb4" />

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

更新 1:

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

        if( isset( $_POST["cb"] ) ) {

            $update = "";

            foreach ($_POST['cb'] as $key => $value) {

                if ( $update ) $update.= ', ';

                $update .= $key . " = 1";

            }

            echo "update table1 set " . $update . " where uid = 10";

        }

    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb[col1]" />
            <input type="checkbox" name="cb[col2]" />
            <!-- all the way to 50 -->
            <input type="checkbox" name="cb[col3]" />
            <input type="checkbox" name="cb[col4]" />

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

原始问题:

我有一个 PHP 表单,其中有很多复选框,允许用户选择要打开的选项或关闭。

返回选中的复选框以便我可以将数据作为 0 或 1 插入数据库的最佳方法是什么?

我有以下代码,但如果我对所有 50 个复选框都使用此方法,这似乎有点过多:

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        if( isset( $_POST["cb1"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }

        if( isset( $_POST["cb2"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }

        // all the way to 50

        if( isset( $_POST["cb49"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }

        if( isset( $_POST["cb50"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }
    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb1" />
            <input type="checkbox" name="cb2" />
            <!-- all the way to 50 -->
            <input type="checkbox" name="cb49" />
            <input type="checkbox" name="cb50" />

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

UPDATE 2:

The following gives me UDPATE table SET db_field1=0, dbfield2=0, dbfield3=0, dbfield4=0 WHERE 1=1 no matter which options are checked:

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

        $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4');

        $update = '';

        foreach($fields as $dbfield => $field) {
            if ($update) $update.= ',';

            $update.= ' '.$dbfield.'=';

            if (isset($_POST[field])) {
                $update.= 1;
            } else {
                $update.= 0;
            }
        }

        echo 'UDPATE table SET'.$update.' WHERE 1=1';

    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb1" />
            <input type="checkbox" name="cb2" />
            <!-- all the way to 50 -->
            <input type="checkbox" name="cb3" />
            <input type="checkbox" name="cb4" />

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

UPDATE 1:

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

        if( isset( $_POST["cb"] ) ) {

            $update = "";

            foreach ($_POST['cb'] as $key => $value) {

                if ( $update ) $update.= ', ';

                $update .= $key . " = 1";

            }

            echo "update table1 set " . $update . " where uid = 10";

        }

    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb[col1]" />
            <input type="checkbox" name="cb[col2]" />
            <!-- all the way to 50 -->
            <input type="checkbox" name="cb[col3]" />
            <input type="checkbox" name="cb[col4]" />

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

ORIGINAL QUESTION:

I have a PHP form with lots of checkboxes to allow users to select which options to turn on or off.

What is the best way to return which checkboxes are checked so I can insert the data into the database as either 0's or 1's?

I have the following code, but this seems a bit excessive if I used this method for all 50 checkboxes:

<?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        if( isset( $_POST["cb1"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }

        if( isset( $_POST["cb2"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }

        // all the way to 50

        if( isset( $_POST["cb49"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }

        if( isset( $_POST["cb50"] ) ) {
            // insert 1 in relevant database table cell
            echo 1;         
        } else {
            // insert 0 in relevant database table cell
            echo 0;
        }
    }
?>

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form method="post">
            <input type="checkbox" name="cb1" />
            <input type="checkbox" name="cb2" />
            <!-- all the way to 50 -->
            <input type="checkbox" name="cb49" />
            <input type="checkbox" name="cb50" />

            <input type="submit" value="submit" />
        </form>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧街凉风2024-12-01 20:01:16

定义复选框名称(POST数组条目名称)和mysql表名称之间的映射;使用它来构建 SQL 来执行值插入。

Define a map between the checkbox name (POST array entry name) and the mysql table name; use that to construct your SQL to do the value insertion.

静水深流2024-12-01 20:01:16

创建一个包含所有不同字段的数组:

$fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'etc', );

$update = '';

foreach($fields as $dbfield => $field) {
    if ($update) $update.= ',';

    $update.= ' '.$dbfield.'=';

    if (isset($_POST[$field])) {
      $update.= 1;
    } else {
      $update.= 0;
    }
}

$query = 'UDPATE table SET'.$update.' WHERE 1=1';

其中 db_field 是表中列的名称,cd 是 html 字段名称

编辑

添加了更好的示例代码

编辑 2

固定帖子多变的

Create an array with all the different fields:

$fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'etc', );

$update = '';

foreach($fields as $dbfield => $field) {
    if ($update) $update.= ',';

    $update.= ' '.$dbfield.'=';

    if (isset($_POST[$field])) {
      $update.= 1;
    } else {
      $update.= 0;
    }
}

$query = 'UDPATE table SET'.$update.' WHERE 1=1';

Where db_field is the name of the column in the table and cd are the html field names

EDIT

Added better sample code

EDIT 2

Fixed post variable

汐鸠2024-12-01 20:01:16

在 HTML 中执行以下操作:

<input type="checkbox" name="checkboxeslist[1]" />
<input type="checkbox" name="checkboxeslist[2]" />
<input type="checkbox" name="checkboxeslist[49]" />
<input type="checkbox" name="checkboxeslist[50]" />

在 PHP 中执行以下操作:

<?php
     if (isset($_POST['checkboxeslist'])) {
          //one is at least selected; hence start looping
          foreach ($_POST['checkboxeslist'] as $key => $value) {
               echo $key; //this will print 1,2,49,50 etc... ONLY IF they have been selected
               //do the DB call here
          }
     }
?>

为了也获得未选中的复选框,请执行以下操作(仅当您想要 1 和 50 之间的所有数字时:

<?php
     if (isset($_POST['checkboxeslist'])) {
          //one is at least selected; hence start looping
          for ($i = 1; $i <= 50; $i++) {
               if (isset($_POST['checkboxeslist'][$i])) {
                   echo "1";
               } else {
                   echo "0";
               }
          }
     }
?>

in HTML do the following:

<input type="checkbox" name="checkboxeslist[1]" />
<input type="checkbox" name="checkboxeslist[2]" />
<input type="checkbox" name="checkboxeslist[49]" />
<input type="checkbox" name="checkboxeslist[50]" />

in PHP do the following:

<?php
     if (isset($_POST['checkboxeslist'])) {
          //one is at least selected; hence start looping
          foreach ($_POST['checkboxeslist'] as $key => $value) {
               echo $key; //this will print 1,2,49,50 etc... ONLY IF they have been selected
               //do the DB call here
          }
     }
?>

In order to also get the non selected checkboxes do the following (only if you want all the numbers between 1 & 50:

<?php
     if (isset($_POST['checkboxeslist'])) {
          //one is at least selected; hence start looping
          for ($i = 1; $i <= 50; $i++) {
               if (isset($_POST['checkboxeslist'][$i])) {
                   echo "1";
               } else {
                   echo "0";
               }
          }
     }
?>
话少情深2024-12-01 20:01:16

对于 HTML,您也可以循环访问这些字段。在某处有一个列列表;使用 MVC 模型,您可以从模型中获取它们。否则,一个数组就足够了,或者查询数据库有关表的内容。

从数据库中获取列名:

 SELECT name FROM syscolumns 
 WHERE id = (SELECT id FROM sysobjects 
 WHERE name= 'MyTableName') 
 ORDER by colorder

然后,

foreach($columns as $column) {
     echo '<input type="text" name="cb['. $column .']" />' . "\n";
}

打印列。

For HTML, you can just loop through those fields too. Have a list of the columns somewhere; with MVC model you could take them from model. Otherwise an array would suffice, or querying database about contents of the table.

For taking column names from database:

 SELECT name FROM syscolumns 
 WHERE id = (SELECT id FROM sysobjects 
 WHERE name= 'MyTableName') 
 ORDER by colorder

Then,

foreach($columns as $column) {
     echo '<input type="text" name="cb['. $column .']" />' . "\n";
}

to print the columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文