多次插入到MySQL从$ _POST数组 [英] Multiple INSERT INTO MySQL from $_POST array
问题描述
我从一种形式收到以下数组:
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循环。因此,可以说您已经命名的复选框姓名=复选框[]。
Well you can try looping with a foreach on that array. So lets say you have named your checkboxes as name="checkbox[]".
然后在页面,您正在处理的$ _POST瓦尔你可以做
Then in the page where you are processing the $_POST vars you can do
foreach($_POST[checkbox] as $box){
//process each checkbox here
$sql="Insert into <table>(<column1>,<column2>) Values (?,?)"
$stmt = $mysqli->prepare(sql);
$stmt->bind_param('<data dtype>', box);
$stmt->bind_param('<data dtype>', other value); // bind $box value to the parameter
$stmt->execute();
}
嗯,这只是一个伪code,让你开始。
我希望你得到的总体思路,继续着与您的项目:)
Well this is just a pseudo-code to get you started. I hope you get the general idea and proceed forward with your project :)
您可以找到$ P $更多的信息在这里ppared声明: 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
这篇关于多次插入到MySQL从$ _POST数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!