简体   繁体   中英

How to loop through arrays to insert into database

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>

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

}


?>

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.

Any ideas?

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

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:

My php handler:

<?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";


 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM