Use foreach in a table checkbox

Asked

Viewed 288 times

0

In the code that I am creating there is a view of all the users of the site in an html table, until then everything right, however when selecting multiple checkbox to delete several users of the site simply nothing happens only the page updates but the data is still 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 in the code there is some part missing, in case this happens was the fact of having cut it so that it was not too extensive !

1 answer

1


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

Pass the <form method="POST"> further 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"];

     ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.