Use foreach in checkbox of a table

0

In the code I'm creating there is a display of all users of the site in an html table, so alright, but selecting multiple checkbox to exclude multiple users from the site simply nothing happens only the page updates but the data still continues there, code I created:

<?php
include("connection.php");
session_start();
?>
    <table width="100%">
        <thead>
            <tr>
                <th>
                    Sel.
                </th>
                <th>
                    ID
                </th>
                <th>
                    Permissão
                </th>
                <th>
                    Status
                </th>
            </tr>
        </thead>
        <tbody>
        <?php
            $select = $mysqli->query("SELECT * FROM usuarios ORDER BY ID ASC");
            $row = $select->num_rows;
            if ( $row > 0 ) {
                while ($get = $select->fetch_array()) {
                $data_mysql = $get["LogouUltimoSite"];
                $timestamp = strtotime($data_mysql);
                $status = $get["Status"];
                $permissao = $get["Permissao"];
       ?>
            <tr>
                <td>
                    <input type="checkbox" name="chk[]" value="<?= $get["ID"] ?>"/>
                </td>
                <td>
                    <?= $get["ID"] ?>
                </td>
                <td>
                    <?php
                        if ( $permissao == 0 ) {
                            echo("Membro");
                        } else {
                            echo("Admin.");
                        }
                   ?>
                </td>
                <td>
                    <?php
                        if ( $status == 0 ) {
                            echo("Normal");
                        } else {
                            echo("Banido");
                        }
                    ?>
                </td>
            </tr>
            <?php
             }
         } else {
         ?>
            <h4> Não existe nenhum usuário ! </h4>
         <?php
     }
         ?>
        </tbody>
     </table>
    <form method="POST">
        <input class="button" type="submit" name="button" value="Excluir"/>
    </form>
    <?php
     $chk = $_POST["chk"];
     if ( isset($_POST["button"]) ) {
         foreach ($chk as $item) {
            $mysqli->query("DELETE FROM usuarios WHERE ID='" . $item . "'");
            $mysqli->close();
     }
}
?>

Thank you in advance if you can help me.

  

It may be that there is some missing part of the code, in case this happened because it was cut off so it would not be too long!

    
asked by anonymous 02.01.2016 / 22:20

1 answer

1

Your checkboxes are being generated before opening form , so they are not sent.

Pass <form method="POST"> up:

<?php
include("connection.php");
session_start();
?>

<form method="POST">      <!-- BASICAMENTE MUDAMOS ESSA LINHA DE LUGAR -->
    <table width="100%">
        <thead>

        ... AQUI VAI A GERACAO DOS CHECKBOXES ...

        </tbody>
     </table>
     <input class="button" type="submit" name="button" value="Excluir"/>
 </form>
 <?php
     if ( isset($_POST["button"]) ) {
        $chk = $_POST["chk"];

     ...
    
02.01.2016 / 22:24