I have a list of items that are fetched from a mySQL database using PHP. Each item is printed out into a list. Each item has a state "pending" or "completed". The state is saved into the database as a string.
By default all items gets a "pending" state, and I want to select certain items and change their state to "completed" from that table. So I inserted the results table (created using a while loop) into a form, and added a checkbox besides each item.
I want to do something like: "when you submit this form, for each item that has checked this checkbox, change their state to completed".
This is what I've done:
echo '<form action="" method="POST">';
while($x=mysqli_fetch_array($consulta)){
echo '<tr><td>'.$x['id'].'</td> <td>'.$x['email'].'</td> <td>'.$x['fecha'].'</td> <td>'.$x['monto'].'</td> <td>'.$x['sucursal'].'</td> <td>'.$x['ticket'].'</td> <td>'.$x['cuentaOrigen'].'</td> <td>'.$x['estado'].' <input name="cambiar[]" type="checkbox" value="'.$x['id'].'"></td>
</tr>';
};
echo '
</tbody>
</table>
<p class="text-right"><input type="submit" class="btn btn-danger" value="Finiquitar los tildados"></input></p>
';
Now this is the part where it should grab each checkbox and change their state. I've tried to adapt this example, but can't make it work.
if(isset($_POST['submit'])) {
$total=array();
$total=count($_POST['cambiar']);
for($i=0;$i<$total;$i++){
$id=$_POST['cambiar'];
$cambiar = "UPDATE datos
SET estado = 'pasado'
WHERE id='$id'";
$resultado = mysqli_query($conexion, $cambiar);
}
}
By the way, the select query that shows the list in the first place is:
$query="SELECT id,
email,
fecha,
monto,
cuentaOrigen,
sucursal,
ticket,
estado
FROM datos
WHERE estado = 'pendiente'
ORDER BY id DESC
";