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?