How to delete multiple clients per checkbox?

2

I'd like to know what I can do to delete more than one client as selected by checkbox . I have inside a complete form:

<form method="post" action="../sys/del.php">
                      <?php
                        require "../sys/conexao.php";
                        $sql = mysqli_query($mysqli, "SELECT * FROM contato");
                        while ($aux = mysqli_fetch_array($sql)) {
                        $id = $aux['id_contato']; 
                        $nome = $aux['nome'];
                        $email = $aux['email'];
                        $assunto = $aux['assunto'];
                        $mes = $aux['mensagem'];
                        $hora = $aux['hora'];

                        print"
                        <tr>
                          <td><input type=\"checkbox\" name=\"valor[]\" id=\"$id\" value=\"$id\"/></td>
                          <td class=\"mailbox-star\"><a href=\"\"><i class=\"fa fa-star text-yellow\"></i></a></td>
                          <td class=\"mailbox-name\"><a href=\"read-mail.html\">$nome</a></td>
                          <td class=\"mailbox-subject\"><b>Kamilla Peliculas - Contato</b> - $assunto</td>
                          <td class=\"mailbox-attachment\"></td>
                          <td class=\"mailbox-date\">$hora</td>
                        </tr>";}
                        mysqli_close($mysqli);
                        ?>                        
                      </tbody>
                    </table><!-- /.table -->
                  </div><!-- /.mail-box-messages -->
                </div><!-- /.box-body -->
                <div class="box-footer no-padding">
                  <div class="mailbox-controls">
                    <!-- Check all button -->
                    <div class="btn-group">
                      <button type="submit" name="excluir" class="btn btn-default btn-sm"><i class="fa fa-trash-o"></i></button>
                    </div>
                    </form>

And this form tells the page that it will get the value of these checkbox selected and delete, however it gives the following error:

  

Notice: Undefined index: value in C: \ xampp \ htdocs \ new \ sys \ del.php on   line 5

     

Warning: Invalid argument supplied for foreach () in   C: \ xampp \ htdocs \ new \ sys \ del.php on line 5

     

Notice: Undefined variable: val in C: \ xampp \ htdocs \ new \ sys \ del.php on   line 12

Someone can help me with this problem, I can not find the error.

PHP page code:

if(isset($_POST['excluir'])){
    require_once"conexao.php";

    foreach($_POST['valor'] as $ck){
        $val = $v;
        mysqli_query($mysqli, "DELETE  FROM contato WHERE id_contato = '$val'");
    echo"<script type=\"text/javascript\">alert(\"Campo Deletado Com Sucesso!\");
            history.go(-1);</script>\n";
            mysqli_close($mysqli);
    }
}
    
asked by anonymous 22.12.2015 / 22:04

1 answer

4

Possible correction:

if( isset( $_POST['excluir'] ) && isset( $_POST['valor'] ) ) {
    require_once"conexao.php";

    foreach($_POST['valor'] as $val){
        mysqli_query($mysqli, "DELETE  FROM contato WHERE id_contato = '$val'");
        echo 'Deletando' .$val. '<br>';

    }
    mysqli_close($mysqli);
} else {
   echo 'Dados não recebidos<br>';
}

You were using a variable that did not exist. I took it and took that echo from the middle of the loop , it was in the wrong place and was going to generate a script for each record to delete.

There are several items that were observed by @rray in a response that was removed. But if it does restore, my +1 is already there because it's a critical part of solving the problem;)

In this way, I recommend taking a look at in this post , which has a more complete solution to make each query delete multiple records at once.

Here is a suggested improvement for readability of form :

<form method="post" action="../sys/del.php">
.. aqui vai o cabeçalho da tabela, depois da abertura do form..
<?php
   require "../sys/conexao.php";
   $sql = mysqli_query($mysqli, "SELECT * FROM contato");
   while ($aux = mysqli_fetch_array($sql)) {
   $id = $aux['id_contato']; 
   $nome = $aux['nome'];
   $email = $aux['email'];
   $assunto = $aux['assunto'];
   $mes = $aux['mensagem'];
   $hora = $aux['hora'];

   echo <<<EOB
         <tr>
            <td><input type="checkbox" name="valor[]" id="$id" value="$id"/></td>
            <td class="mailbox-star"><a href=""><i class="fa fa-star text-yellow"></i></a></td>
            <td class="mailbox-name"><a href="read-mail.html">$nome</a></td>
            <td class="mailbox-subject"><b>Kamilla Peliculas - Contato</b> - $assunto</td>
            <td class="mailbox-attachment"></td>
            <td class="mailbox-date">$hora</td>
         </tr>
EOB;
mysqli_close($mysqli);
?>             
      </tbody>
   </table><!-- /.table -->

      .. etc ...
</form>

The change here was to put <form> and </form> out of all table markup, and use HEREDOC for easy reading of the PHP part.

    
22.12.2015 / 22:11