Delete button does not work

0

I created a delete button that is on the line of every record I'm displaying in a table, but when I click on it and display the delete screen, I realize that the last record in the table is always selected regardless of which item I click. Below is my code, thanks for solution suggestions!

Exclusion screen:

<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <form role="form" method="POST" action="php/excluir.php">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Excluir </h4>
      </div>
      <div class="modal-body">
        Você tem certeza que deseja excluir?
        <?php
          $result = mysqli_query($conexao, "SELECT id FROM itens WHERE descricao = 'descricao'");
          while($exibir = mysqli_fetch_array($result)){
            $id = $exibir['id'];
            echo '<input type="hidden" name="id" value="' . $id . '"/>';
          } 
        ?>
    </div>
        <div class="modal-footer">
          <button data-dismiss="modal" class="btn btn-default" type="button" data-dismiss="modal">Não</button>
          <button class="btn btn-warning" type="submit">Sim</button>
        </form>    
        </div>
    </div>
  </div>
</div>

delete.php

<?php
$id = $_POST['id'];

$conexao = mysqli_connect('', '', '', '');
if (!$conexao) {
    echo "<script> window.location.replace('../erro.html'); </script>"; 
}

$result  = mysqli_query($conexao, "DELETE from itens WHERE id = '$id'");
$result  = mysqli_query($conexao, "DELETE from tabela WHERE id = '$id'");

if ($result) {
    echo "<script> window.location.replace('../home.php'); </script>";
}else{
    echo "<script> window.location.replace('../erro.html'); </script>";     
}

mysqli_close($conexao);
    
asked by anonymous 18.04.2017 / 23:43

2 answers

0

Here it takes all loop IDs you make there in the modal and excludes:

HTML

<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <form role="form" method="POST" action="php/excluir.php">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Excluir </h4>
      </div>
      <div class="modal-body">
        Você tem certeza que deseja excluir?
        <?php
          $result = mysqli_query($conexao, "SELECT id FROM itens WHERE descricao = 'descricao'");
          while($exibir = mysqli_fetch_array($result)){
            $id = $exibir['id'];
            echo '<input type="hidden" name="id[]" value="' . $id . '"/>';
          } 
        ?>
    </div>
        <div class="modal-footer">
          <button data-dismiss="modal" class="btn btn-default" type="button" data-dismiss="modal">Não</button>
          <button class="btn btn-warning" type="submit">Sim</button>
        </form>    
        </div>
    </div>
  </div>
</div>

PHP

<?php
$ids = $_POST['id'];

$conexao = mysqli_connect('', '', '', '');
if (!$conexao) {
    echo "<script> window.location.replace('../erro.html'); </script>"; 
}

if(!empty($ids)){

  foreach($ids as $id){

    $result  = mysqli_query($conexao, "DELETE from itens WHERE id = '$id'");
    $result  = mysqli_query($conexao, "DELETE from tabela WHERE id = '$id'");

  }

}

if ($result) {
    echo "<script> window.location.replace('../home.php'); </script>";
}else{
    echo "<script> window.location.replace('../erro.html'); </script>";     
}

mysqli_close($conexao);
    
18.04.2017 / 23:49
0

You need to turn checkboxes into an array by adding brackets in the name attribute.

You must add brackets [] to the checkbox name otherwise only the last value is sent.

When you put a "name" with square brackets it is sent as a vector or array to the receiver.

The solution given by the friend Alisson Acioli is in this sense.

  

Considerations

In the name attribute, at the end of its name, we put brackets, indicating that more than one value can be sent to the same field. We can use this notation for other fields too, such as text fields

<input type="text" name="nome[] value="Fulano" />
<input type="text" name="nome[] value="Ciclano" />

And if we submit the form with these elements, we have the following result

Array
(
    [nome] => Array
         (
            [0] => Fulano
            [1] => Ciclano
         )
)

We can have this behavior for all fields in form, text, radio, checkbox etc ...

  

Treating multiple checkbox fields of the same name with PHP

<?php
 $ids= $_POST['id'];

   if (!empty($ids)) {                
       $qtd = count($ids);
       for ($i = 0; $i < $qtd; $i++) {
         $result  = mysqli_query($conexao, "DELETE from itens WHERE id = '".$ids[$i]."'");
         $result  = mysqli_query($conexao, "DELETE from tabela WHERE id = '".$ids[$i]."'");
       }
   }

?>
    
19.04.2017 / 00:43