0

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'].'&nbsp;&nbsp;<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
      ";
Community
  • 1
  • 1
Rosamunda
  • 14,620
  • 10
  • 40
  • 70
  • 1
    Firstly, this will never happen `if(isset($_POST['submit'])) {...}` you've no name attribute to match it for the submit button. Which I might you can get rid of `` it's not a valid closing tag. Error reporting would have been of help here. http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner May 11 '16 at 01:55
  • I remember you posted a question related to this earlier where I gave you a link for it to adapt from a delete to an update but have deleted it; why is that? plus, where's the closing `` tag? I feel like I've fallen onto deaf ears again. – Funk Forty Niner May 11 '16 at 01:58
  • if you're expecting multiple checkboxes, `name="cambiar[]"`, then treat it as an array `$_POST['cambiar'][0] ...` and so on. just use `$_POST['cambiar'][$i]`, you already have your `for` loop – Kevin May 11 '16 at 02:02
  • Yes, but as someone marked to "close it", I thought about completing it and pot it again. – Rosamunda May 11 '16 at 02:10
  • I've followed your link Fred, and added it to my question. I've tried to follow the solution there and adapt it. It's not duplicated anymore, I've deleted the other one because it received a vote to close it as too broad. – Rosamunda May 11 '16 at 02:15

0 Answers0