php 如何在表格中插入多个复选框值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20176673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:57:28  来源:igfitidea点击:

How do I insert multiple checkbox values into a table?

phpmysqlcheckboxmultipleselection

提问by SirBT

I cant seem to find or figure out a working solution to insert multiple checkbox values from a form into a table. The closes I have come is inserting the value of merely one checkbox value into a table. Kindly point out how I can insert multiple checkbox values and not merely one.

我似乎无法找到或找出将多个复选框值从表单插入表格的可行解决方案。我来的关闭只是将一个复选框值的值插入到表格中。请指出我如何插入多个复选框值,而不仅仅是一个。

Find below what I have so far:

在下面找到我到目前为止所拥有的:

My form:

我的表格:

<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days" value="Daily">Daily<br>
<input type="checkbox" name="Days" value="Sunday">Sunday<br>
<input type="checkbox" name="Days" value="Monday">Monday<br>
<input type="checkbox" name="Days" value="Tuesday">Tuesday <br>
<input type="checkbox" name="Days" value="Wednesday">Wednesday<br>
<input type="checkbox" name="Days" value="Thursday">Thursday <br>
<input type="checkbox" name="Days" value="Friday">Friday<br>
<input type="checkbox" name="Days" value="Saturday">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

My php file to read and insert the values into a table:

我的 php 文件读取值并将其插入表中:

<?php

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$checkBox = $_POST['Days'];

if(isset($_POST['submit']))
{
    for ($i=0; $i<sizeof($checkBox); $i++)
        {
            $query="INSERT INTO example (orange) VALUES ('" . $checkBox[$i] . "')";     

            mysql_query($query) or die (mysql_error() );
        }
    echo "Complete";

}

?>

回答by Viacheslav Kondratiuk

You should specify

你应该指定

<input type="checkbox" name="Days[]" value="Daily">Daily<br>

as array.

作为数组。

Add []to all names Daysand work at php with this like an array.

添加[]到所有名称Days并在 php 中像数组一样使用它。

After it, you can INSERTvalues at different columns at db, or use implodeand save values into one column.

之后,您可以INSERT在 db 的不同列中取值,或者使用implode值并将其保存到一列中。



Didn't tested it, but you can try like this. Don't forget to replace mysqlwith mysqli.

没有测试过,但你可以像这样尝试。不要忘记替换mysqlmysqli.

<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]" value="Daily">Daily<br>
<input type="checkbox" name="Days[]" value="Sunday">Sunday<br>
<input type="checkbox" name="Days[]" value="Monday">Monday<br>
<input type="checkbox" name="Days[]" value="Tuesday">Tuesday <br>
<input type="checkbox" name="Days[]" value="Wednesday">Wednesday<br>
<input type="checkbox" name="Days[]" value="Thursday">Thursday <br>
<input type="checkbox" name="Days[]" value="Friday">Friday<br>
<input type="checkbox" name="Days[]" value="Saturday">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>


<?php

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$checkBox = implode(',', $_POST['Days']);

if(isset($_POST['submit']))
{       
    $query="INSERT INTO example (orange) VALUES ('" . $checkBox . "')";     

    mysql_query($query) or die (mysql_error() );

    echo "Complete";

}

?>

回答by kero

You need to declare the array in the HTML via

您需要通过以下方式在 HTML 中声明数组

<input type="checkbox" name="Days[]" value="Daily">

Also you can insert multiple items with one query like this

您也可以使用这样的一个查询插入多个项目

$query = "INSERT INTO example (orange) VALUES ";
for ($i=0; $i<count($checkBox); $i++)
    $query .= "('" . $checkBox[$i] . "'),";
$query = rtrim($query,',');
mysql_query($query) or die (mysql_error() );


Also keep in mind that mysql_*functions are officially deprecatedand hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SOfor more information.

另请记住,mysql_*函数已正式弃用,因此不应在新代码中使用。您可以改用 PDO 或 MySQLi。有关更多信息,请参阅SO 上的此答案

回答by Drudge Rajen

I think this should work .. :)

我认为这应该有效.. :)

<input type="checkbox" name="Days[]" value="Daily">Daily<br>
<input type="checkbox" name="Days[]" value="Sunday">Sunday<br>

回答by Bear

I think you should $_POST[][], i tried it and it work :)), tks

我认为你应该 $_POST[][],我试过了,它工作 :)),tks