How to delete multiple clients by checkbox?

Asked

Viewed 653 times

2

I would like to know how I can delete more than one customer as selected by checkbox. I have within 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 sends to the page that will get the value of these checkbox selected and delete, but it gives the following error:

Notice: Undefined index: valor in C: xampp htdocs novo 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 novo sys del.php on line 12

Someone can help me with this problem, I can’t find the mistake.

Code of the PHP page:

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);
    }
}
  • I posted a solution to this issue: http://answall.com/questions/103910/delete-em-multiplos-posts-com-checkbox

1 answer

4


Following 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 didn’t exist. I took that echo out of the middle of the loop, it was in the wrong place and would generate a script for each record to delete.

There are several items that were observed by @rray, in a response that has been removed. But if it restores, my +1 is already there, because it is a fundamental part of the solution of the problem ;)

Anyway, I recommend taking a look in that post, that has a more complete solution to make each query delete multiple records at once.

Here is a suggestion for improving the 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 the <form> and </form> out of any table Markup, and the use of HEREDOC for easy reading of the PHP part.

Browser other questions tagged

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