Delete multiple rows using checkbox in php and mysql

-1

I want to delete the rows that I select with my checkbox and also delete the database so that pq is given in this part of the code:

<?php
  include "conexao.php";
  if (isset($_POST['sel'])) {
    foreach ($_POST['sel'] as $id) {
      $sql="DELETE FROM lojas WHERE id = $id";
      $res=mysqli_query($con,$sql);  
    }
  }else{
    echo "form nao submetido";
  }
?>

Else is appearing on the screen, ie "not submitted form" and not what is in the case

Here is the whole code:

<form  class="customform" method="POST" action="fabrica_conLoja2.php" >

<div  class="s-12 l-7">Nome Loja<input name="nloja" placeholder="Nome Loja"  type="text" /></div>

<div class="s-12 l-7">Nome Gerente<input name="ngerente" placeholder="Nome Gerente" type="text" /></div>


<div class="s-12"></div>
<div class="s-12 m-6 l-4"><button name="cadastrar" type="submit" value="cadastrar" onclick="">Pesquisar</button></div>   
      <title></title>
    </head>
    <body>
    <table>
      <tr>
        <th>Id</th>
        <th>Nome Loja</th>
        <th>Nome Gerente</th>
        <th>Rua</th>
        <th>Bairro</th>
        <th>Número Estabele.</th>
        <th>Estado</th>
        <th>Cidade</th>
        <th>Telefone</th>
         <th>Selecionar</th>
      </tr>
      <br><br><br><br><br><br><br><br><br>
    <?php
        include "conexao.php";
        $nomeLoja=$_POST["nloja"];//"nloja");
        $nomeGerente=$_POST["ngerente"];//"nome_gerente");
        $sql = "SELECT * FROM lojas WHERE nome LIKE '%$nomeLoja%' and nome_gerente LIKE '%$nomeGerente%' ";
        $res=mysqli_query($con,$sql);         
    $li = mysqli_num_rows($res);
    echo " &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp $li loja(s) encontrada(s)<br/>";

      while ($vloj=mysqli_fetch_row($res)) {
            $vid=$vloj[0];
            $vnome=$vloj[1];
            $vgerente=$vloj[2];
            $vrua=$vloj[3];
            $vbairro=$vloj[4];
            $vnumero=$vloj[5];
            $vuf=$vloj[6];
            $vcidade=$vloj[7];
            $vtel=$vloj[8];

        echo"<tr>
               <td>$vid</td>
               <td>$vnome</td>
               <td>$vgerente</td>
               <td>$vrua</td>
               <td>$vbairro</td>
               <td>$vnumero</td>
               <td>$vuf</td>
               <td>$vcidade</td>
               <td>$vtel</td>
               <td align = center><input type='checkbox' value='$vid' name='sel[]'></td>
            </tr>";   
        }
        ?>
    </table> <br> 
     </form>

        <form method="POST" action="fabrica_conLoja2.php">

                     <?php
                   include "conexao.php";
                   if (isset($_POST['sel'])) {
                   foreach ($_POST['sel'] as $id) {
                    $sql="DELETE FROM lojas WHERE id = $id";
                   $res=mysqli_query($con,$sql);  
                   }
                   }else{
                    echo "form nao submetido";
                   }
                   ?>
    
asked by anonymous 27.08.2017 / 18:53

2 answers

0

It has 2 <form> on the page. He is sent the second that has nothing in it.

Delete the second <form> .

Update:

The structure would be this:

<form  class="customform" method="POST" action="fabrica_conLoja2.php" >
    <div  class="s-12 l-7">Nome Loja<input name="nloja" placeholder="Nome Loja"  type="text" /></div>
    <div class="s-12 l-7">Nome Gerente<input name="ngerente" placeholder="Nome Gerente" type="text" /></div>
    <div class="s-12"></div>
    <div class="s-12 m-6 l-4"><button name="cadastrar" type="submit" value="cadastrar" onclick="">Pesquisar</button></div>   
</form>
    <?php
        if ( isset($_POST['ngerente']) && isset($_POST['nloja'])) {
            include "conexao.php";
            $nomeLoja=$_POST["nloja"];//"nloja");
            $nomeGerente=$_POST["ngerente"];//"nome_gerente");
            $sql = "SELECT * FROM lojas WHERE nome LIKE '%$nomeLoja%' and nome_gerente LIKE '%$nomeGerente%' ";
            $res=mysqli_query($con,$sql);         
            $li = mysqli_num_rows($res);
            echo " &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp $li loja(s) encontrada(s)<br/>";
    ?>
    <form  class="customform" method="POST" action="fabrica_conLoja2.php" >
    <table>
      <tr>
        <th>Id</th>
        <th>Nome Loja</th>
        <th>Nome Gerente</th>
        <th>Rua</th>
        <th>Bairro</th>
        <th>Número Estabele.</th>
        <th>Estado</th>
        <th>Cidade</th>
        <th>Telefone</th>
         <th>Selecionar</th>
      </tr>
      <br><br><br><br><br><br><br><br><br>

    <?php
      while ($vloj=mysqli_fetch_row($res)) {
            $vid=$vloj[0];
            $vnome=$vloj[1];
            $vgerente=$vloj[2];
            $vrua=$vloj[3];
            $vbairro=$vloj[4];
            $vnumero=$vloj[5];
            $vuf=$vloj[6];
            $vcidade=$vloj[7];
            $vtel=$vloj[8];

        echo"<tr>
               <td>$vid</td>
               <td>$vnome</td>
               <td>$vgerente</td>
               <td>$vrua</td>
               <td>$vbairro</td>
               <td>$vnumero</td>
               <td>$vuf</td>
               <td>$vcidade</td>
               <td>$vtel</td>
               <td align = center><input type='checkbox' value='$vid' name='sel[]'></td>
            </tr>";   
        }
        ?>
        <tr>
            <td colspan="10">
                <button name="apagar" type="submit" value="Apagar">Apagar</button>
            </td>
        </tr>
    </table><br> 
    </form>
    <?php
        }
       if (isset($_POST['sel'])) {
            include "conexao.php";
            foreach ($_POST['sel'] as $id) {
            $sql="DELETE FROM lojas WHERE id = $id";
            $res=mysqli_query($con,$sql);  
            }
       }else{
        echo "form nao submetido";
       }
   ?>

See that I created another <form> and a <button> for the set of checkboxes.

    
27.08.2017 / 19:06
0

The name of the input is as sel[] , ie all inputs will come out with the same name. Try to put in the following checkbox

Name= sel[".$vid."]

This way you will have $ _POST as the multi-dimensional array you want.

    
27.08.2017 / 21:10