I'm having an issue with a PHP form doing an update one row at a time. It will work for the first row no problem, but the next rows don't get all the variables. For example, row 1 will have company1, checked paid and not checked for deactivate and it gets and updates all variables. But company2's row only gets the ID and the default values for the checkboxes. For some reason it doesn't pull the info for the whole row, just the ID. Any help would be appreciated!
This is my Form (edited):
<html>
<body>
<form action="next.php" method="POST">
<table border="1" align="center">
<tr>
<th>
Name
</th>
<th>
Address
</th>
<th>
Paid
</th>
<th>
Active
</th>
</tr>
<?php
require_once ("connect.php");
require_once ("phpfunctions.php");
error_reporting(0);
$cd=companyDisplay();
$result = mysqli_query($mysqli,$cd);
while($res=mysqli_fetch_array($result))
{
$final[]=$row;
?>
<tr>
<td class="name">
<a href='edit.php?ID="<?php echo $res['ID']; ?>"'>
<?php echo $res['name'];?>
</a>
</td>
<td class="Address">
<?php echo $res['Address'];?>
</td>
<td class="paid">
<?php foreach($final as $k => $v){ ?>
<input type="checkbox" value="0" name="results[<?= $k ?>][paid]"
<?php } ?>
<?php
if($res["paid"]==0)
{
echo "checked";
}
?>>
</td>
<td class="active">
<?php foreach($final as $k => $v){ ?>
<input type="checkbox" value="0" name="results[<?= $k ?>][active]"
<?php } ?>
<?php
if($res["active"]==0)
{
echo "checked";
}
?> >
</td>
<td class="update">
<?php foreach($final as $k => $v){ ?>
<input type="hidden" name="results[<?= $k ?>][ID]" value="<?php echo $res['ID']; ?>">
<?php } ?>
<input type="submit" name="submit" value="Update">
</td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>
This is my next php page where the variables are posted and the query is run:
<?php
require_once ("connect.php");
require_once ("phpfunctions.php");
error_reporting(0);
var_dump($_POST);
//info to store from index.php
$ID=$_POST['ID'];
$ID= str_replace('"', '', $ID);
$paid=(isset($_POST['paid']))? 0 : 1;
$active = (isset($_POST['active']))? 0 : 1;
//adds updated info to database
$uc=updateCompany($paid,$active,$ID);
$r = mysqli_query($mysqli,$uc);
echo "Information stored successfully";
echo $paid;
echo $active;
echo $ID;
?>
And here is my function:
function updateCompany($paid,$active,$ID)
{
$uc="UPDATE
`company`
SET
`paid`='$paid',
`active`='$active'
WHERE
`ID`='$ID'";
return $uc;
}
Output for first row:
array(3) { ["paid"]=> string(1) "0" ["ID"]=> string(2) "12" ["submit"]=> string(6) "Update" } Information stored successfully 0(paid) 1(deactivate) 12(id)
output for next row:
array(2) { ["ID"]=> string(2) "13" ["submit"]=> string(6) "Update" } Information stored successfully 1(paid) 1(deactivate) 13(ID)
Both have same choices set and should be same except for the ID. Where am I going wrong here?