Delete multiple Rows using php and mysql checkbox

Asked

Viewed 110 times

-1

I want to delete the Rows that I select with my checkbox and delete also in the database so that is giving pq 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";
  }
?>

Is appearing on the screen the Else or is "not submitted form" and not what is in the if

Here 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";
                   }
                   ?>
  • 1

    Just one observation: I think $id should be in single quotes in the query, like this: ...WHERE id = '$id'.

  • @Davidsamm Not necessarily depends on how he created the table. If Id is INT it should not be in quotes, if varchar should.

  • You could report the error that appears?

  • @Renatosilva Legal!

  • @Renatosilva The error in the code is structural. In my answer is working normal. It was not sending a second <form> with the checkbox.

2 answers

0

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

Erase the second <form>.

Updating:

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.

  • I took it and the mistake continues

  • And I put the single quotes in the $id and nothing

  • Where are you submitting the checkboxes? In the first <form> you search and return a list with checkboxes, right? Then you need to mark the boxes you want to delete and then resubmit a <form> with these boxes.

0

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

Name= sel[".$vid."]

This way you’ll get the $_POST as the multi dimensional array you want.

Browser other questions tagged

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