如何通过阵列插入到数据库环 [英] How to loop through arrays to insert into database
问题描述
我有三个阵列和我想要添加的阵列位置[0],那么[1],然后按[2]数组等等,所以我可以为三个字段到我的数据库插入值。
I have three arrays and I want to add the arrays in position [0], then [1], then [2] etc of the arrays so I can insert the values for the three fields into my database.
首先,我连接到我的数据库,并得到我的结果:
First I connect to my DB and get my results:
<?php
require_once("con.php");
$p = payment();
$result = mysqli_query($mysqli, $p);
?>
然后我建立我的方式:
Then I build my form:
<form>
<table>
<?php while ($res = mysqli_fetch_array($result)) { ?>
<tr>
<td class="paid">
<input type="hidden" value="1" name="paid[<?php echo $res['name']; ?>]">
<input type="checkbox" value="0" name="paid[<?php echo $res['name']; ?>]"
<?php
if ($res["paid"] == 0) {
echo "checked";
}
?>>
</td>
<td class="active">
<input type="hidden" value="1" name="active[<?php echo $res['name']; ?>]">
<input type="checkbox" value="0" name="active[<?php echo $res['name']; ?>]"
<?php
if ($res["active"] == 0) {
echo "checked";
}
?> >
</td>
<input type="hidden" name="ID[<?php echo $res['name']; ?>]" value="<?php echo $res['ID']; ?>">
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
这是我的PHP处理程序:
And this is my PHP handler:
<?php
function updatePayment($paid, $active, $ID) {
$uc = "UPDATE `company` SET `paid`='$paid', `active`='$active' WHERE `ID`='$ID'";
return $uc;
}
$paid = $_POST['paid'];
$active = $_POST['active'];
foreach ($_POST as $key => $value) {
$ID = $ID[$key];
$paid = $paid[$key];
$active = $active[$key];
$up = updatePayment($paid, $active, $ID);
$r = mysqli_query($mysqli, $up);
echo "Information stored successfully";
}
?>
现在它不插入任何内容,我可以看到阵列在他们正确的数据与的var_dump($ _ POST)
;但我似乎无法得到它插入到数据库中。我已经尝试了一些为
循环,的foreach
但不知道我在做是正确的。
Right now it's not inserting anything, I can see the arrays have the proper data in them with var_dump($_POST)
; but I can't seem to get it to insert into the database. I've tried some for
loops, foreach
but not sure I'm doing it right.
任何想法?
的var_dump表明这一点:
var_dump shows this :
array(4) {
["paid"]=> array(3) {
["CompanyA"]=> string(1) "0"
["CompanyB"]=> string(1) "0"
["CompanyC"]=> string(1) "0"
}
["active"]=> array(3) {
["CompanyA"]=> string(1) "1"
["CompanyB"]=> string(1) "1"
["CompanyC"]=> string(1) "0"
}
["ID"]=> array(3) {
["CompanyA"]=> string(2) "12"
["CompanyB"]=> string(2) "13"
["CompanyC"]=> string(2) "14"
}
["submit"]=> string(6) "Update"
}
有没有办法把所有的公司A一起到一个数组更新或所有阵列[O]位置的一个数组,那么[1],等等?
Is there a way to group all CompanyA together to into an array to update or an array of all arrays [o] position, then [1], etc?
推荐答案
这是我想出了答案。希望这可以帮助别人的未来:
This is the answer I came up with. Hopefully it helps someone in the future:
我的PHP处理程序:
<?php
require_once("con.php");
$ID=$_POST['ID'];
$paid=$_POST['paid'];
$active=$_POST['active'];
$count=count($ID);
$n= implode(", ", $ID);
$ID=explode(',',$n);
$p=implode(", ", $paid);
$paid=explode(',',$p);
$a=implode(", ", $active);
$active=explode(',',$a);
$i=0;
for ($i; $i<$count; $i++)
{
echo $paid[$i];
echo $active[$i];
echo $ID[$i];
//adds updated info to database
$uc=updateCompany($paid[$i],$active[$i],$ID[$i]);
$r = mysqli_query($mysqli,$uc);
echo "Information stored successfully";
}
这篇关于如何通过阵列插入到数据库环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!