Delete button does not work

Asked

Viewed 203 times

0

I created a delete button that sits on the row of each record that I am displaying in a table, but when I click on it and display the delete screen, I realize that the last record of the table is always selected, regardless of the item that I click. Below follows my code, thank you 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);
  • Is 1 modal for 1 record ? Or 1 modal for multiple records ?

  • The modal is for the selected record, only 1

2 answers

0

Here it takes all the Ids of the loop that you do there in modal and deletes:

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);
  • I don’t understand, it works to delete only 1?

  • no. It can exclude multiple, just be selected.

  • I know, but I wonder if to delete 1 only, works this way too

  • yes, just select the one you want to delete

0

You need to turn checkboxs into an array by adding [] square brackets to the attribute name.

It is necessary to add brackets[] in the checkbox name otherwise only the last value is sent.

When you place a bracketed "name" it is sent in vector or array form to the receiver.

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

Considerations

In the name attribute, at the end of its name, we place square brackets indicating that more than one value can be sent to the same field. We can use this notation for other fields as well, for example 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 existing in the form, text, radio, checkbox etc...

Handling 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]."'");
       }
   }

?>

Browser other questions tagged

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