Multiple INSERT INTO MySQL from $_POST array(来自 $_POST 数组的多个 INSERT INTO MySQL)
问题描述
我从一个表单中收到以下数组:
I am receiving the following arrays from a form:
array (size=1)
'checkbox' =>
array (size=2)
0 => string '14' (length=2)
1 => string '13' (length=2)
2 => string '15' (length=2)
array (size=1)
'id' => string '1' (length=1)
我需要构建一个如下所示的查询:
I need to build a query looking like this:
$sql = "INSERT INTO table(column1,column2) VALUES (14,1),(13,1),(15,1)";
根据选中的复选框,第一个数组每次都会有所不同.
And the first array will be different every time based on the checked checkboxes.
推荐答案
好吧,您可以尝试在该数组上使用 foreach 进行循环.因此,假设您已将复选框命名为 name="checkbox[]"
.
Well, you can try looping with a foreach on that array. So let's say you have named your checkboxes as name="checkbox[]"
.
然后在您正在处理 $_POST
变量的页面上,您可以执行此操作
Then on the page where you are processing the $_POST
vars you can do
$sql = "INSERT INTO table(column1,column2) VALUES (?,?)";
$stmt = $mysqli->prepare($sql);
foreach ($_POST['checkbox'] as $box) {
//process each checkbox here
$stmt->bind_param('ss', $box, $otherValue);
$stmt->execute();
}
这只是一个让您入门的伪代码.
This is just a pseudo-code to get you started.
您可以在此处找到有关准备好的语句的更多信息:http:///php.net/manual/en/mysqli-stmt.bind-param.php
You can find more info on prepared statements here: http://php.net/manual/en/mysqli-stmt.bind-param.php
这篇关于来自 $_POST 数组的多个 INSERT INTO MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:来自 $_POST 数组的多个 INSERT INTO MySQL


- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01