如何循环遍历数组以插入数据库

时间:2015-08-20 16:40:37

标签: php mysql arrays forms checkbox

我有三个数组,我想在数组的位置[0],然后[1],然后[2]等处添加数组,这样我就可以将三个字段的值插入到我的数据库中。

首先,我连接到我的数据库并获得结果:

<?php
require_once("con.php");
$p = payment();
$result = mysqli_query($mysqli, $p);
?>

然后我建立我的表格:

<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处理程序:

<?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)的数据;但我似乎无法将其插入数据库。我尝试了一些for循环,foreach但不确定我做得对。

有什么想法吗?

var_dump显示了这个:

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" 
} 

有没有办法将所有CompanyA组合成一个数组来更新或所有数组[o]位置,然后[1]等数组?

1 个答案:

答案 0 :(得分:1)

这是我想出的答案。希望它可以帮助将来的某个人:

我的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";


 }