I need to update multiple records at the same time with php / postgresql (also for mysql) through a checkbox and textbox but I can not.
The code below only updates the last record's sequence field and does not update the status. The remaining records, nothing is updated.
My code:
<form action="" method="post" name="form" style="width:100%; height:100%;">
<div style="overflow-y:scroll; width:100%; height:90%;">
<?php
$query = pg_query($dbconn,"select *, case when idstatus = 1 then 'checked=\"checked\"' else '' end as status1 from tabela") or die(pg_last_error($dbconn));
?>
<table width="100%" border="0" cellspacing="20" cellpadding="0" align="center">
<tr>
<?php
$loop = 3;
$i = 1;
while($queryRow = pg_fetch_array($query)){
if($i < $loop){
echo '
<td valign="middle" align="center">
<img src="imagem/'.$queryRow['imagem'].'" width="250" height="150" /><br />
<p>
<table width="250" border="0" cellspacing="5" cellpadding="0">
<tr>
<td align="right" valign="middle" width="33%">STATUS</td>
<td style="padding:5px;" align="left" valign="middle" width="33%"><input name="status" type="checkbox" value=1" '.$queryRow ['status1'].' /></td>
<td style="padding:5px;" align="center" valign="middle" width="34%"><input name="sequencia" type="text" value="'.$queryRow['sequencia'].'" /><input name="id" type="hidden" value="'.$queryRow['id'].'" /></td>
</tr>
</table>
</p>
</td>
';
}elseif($i = $loop){
echo '
<td valign="middle" align="center">
<img src="imagem/'.$queryRow['imagem'].'" width="250" height="150" /><br />
<p>
<table width="250" border="0" cellspacing="5" cellpadding="0">
<tr>
<td align="right" valign="middle" width="33%">STATUS</td>
<td style="padding:5px;" align="left" valign="middle" width="33%"><input name="status" type="checkbox" value=1" '.$queryRow ['status1'].' /></td>
<td style="padding:5px;" align="center" valign="middle" width="34%"><input name="sequencia" type="text" value="'.$queryRow['sequencia'].'" /><input name="id" type="hidden" value="'.$queryRow['id'].'" /></td>
</tr>
</table>
</p>
</td>
</tr>
</table>
</p>
</td>
</tr>
<tr>
';
$i = 0;
}
$i++;
}
?>
</tr>
</table>
</div>
<div style="width:100%; height:10%; background:none;">
<table width="100%" height="100%" border="0" cellspacing="5" cellpadding="0" style="border-top:#000099 solid 3px;">
<tr>
<td align="center" valign="middle"><button>ATUALIZAR</button></td>
</tr>
</table>
</div>
<?php
if(isset($_POST)){
$count = count($_POST['id']);
$i = 0;
while ($i < $count) {
$id = $_POST['id'][$i];
$status = $_POST['status'][$i];
$sequencia = $_POST['sequencia'][$i];
$query = pg_query($dbconn,"update tabela set status = $status, sequencia = $sequencia where id = $id") or die(pg_last_error($dbconn));
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';
++$i;
}
}
?>
</form>