Jump to content

Delete registry from DB


Flaviaac
 Share

Recommended Posts

Hi guys,

I'm beginning to learn PHP now, and i have too much doubts yet, so i would like some help. 😃

I want to delete a registry from table when i press the button "Apagar" like show in the picture below:

produtos.png.7defcef61a7206b0a43623710e40c655.png

 

When I press the button, it shows a message saying it was successfully deleted, but in fact it was not deleted in the DB.

The message: Notice: Undefined index: id in .../teste_tabela3/deletar_produto.php on line 11

 

What did i do wrong in my code?

 

code from "deletar_produto.php" file:

<?php
    include_once 'cabeçalho.php';
    include_once 'conexao.php';          
        
    $id = $_POST['id']; 
    if(isset($_POST['apagar'])){ 
      $sql = "DELETE FROM produtos WHERE id='$id'";
      $result = mysqli_query($conn, $sql);
      if($conn->query($sql) === TRUE)  
        echo "<br/><br/><span>O registro foi deletado com sucesso...!!</span>"; 
    }else{
      echo "<p>Não foi possivel apagar o registro....!!</p>";
    }
    $conn->close();    
 ?> 

And this is code of "listar_produtos.php" file:

<?php
    include_once 'cabeçalho.php';
    include_once 'conexao.php';
?>

  <div class="d-flex mx-2 my-2">
    <div class="mr-auto p-2">                                            
      <h2 class="display-4 titulo">Lista de Produtos</h2>                    
    </div>                      
  </div> 

  <?php
    $sql = "SELECT id, produto, valor FROM produtos";
    $result = $conn->query($sql);                    
  ?>

  <!--------------------------------------------TABELA-------------------------------------------->

  <div class="table-responsive">
    <table class="table table-bordered table-hover table-striped">
      <thead>
        <tr>
          <th>ID</th>
          <th>Produto</th>
          <th>Valor</th>
          <th>Ações</th>                   
        </tr>
      </thead>                      
      <?php
      if ($result->num_rows > 0) {
        echo "<tbody>"; 
        while($row = $result->fetch_assoc()) {           
          echo "<tr>";  
            echo "<td>" .$row['id'] ."</td>";  
            echo "<td>" .$row['produto']. "</td>";  
            echo "<td>" .$row['valor']. "</td>"; 
            echo "<td>";                                       
              echo "<form action='deletar_produto.php' method='post'>";
                echo "<button type='submit' class='btn btn-outline-primary' name='editar'>Editar</button> &nbsp;";
                echo "<button type='submit' class='btn btn-outline-primary' name='apagar'>Apagar</button>"; 
              echo "</form>";                                                                                                                 
            echo "</td>";
          echo "</tr>";    
        }                      
      } else {
        echo "0 results";
      }
      ?>                             
      </tbody>
    </table> 

  <?php
  $conn->close();
  include_once 'rodape.php';
  ?>

 

Thank you so much!!

 

 

Link to comment
Share on other sites

You aren't passing your ID along with your edit action.

You need to add a line like this

<?php
echo "<input type='hidden' name='id' value='".$row['id'] ."' />";

 

Rather than Echoing all your output, have you considered the following method?
(<?= ?> is shorthand for <?php echo ?>)

<table class="table table-bordered table-hover table-striped">
  <thead>
    <tr>
      <th>ID</th>
      <th>Produto</th>
      <th>Valor</th>
      <th>Ações</th>
    </tr>
  </thead>
  <?php
  if ($result->num_rows > 0) {
    ?>
    <tbody>
      <?php
      while ($row = $result->fetch_assoc()) {
        ?>
        <tr>
          <td><?= $row['id']; ?></td>";
          <td><?= $row['produto']; ?></td>";
          <td><?= $row['valor']; ?></td>";
          <td>
            <form action='deletar_produto.php' method='post'>
              <input type='hidden' name='id' value='<?= $row['id']; ?>' />
              <button type='submit' class='btn btn-outline-primary' name='editar'>Editar</button>  
              <button type='submit' class='btn btn-outline-primary' name='apagar'>Apagar</button>
            </form>";
          </td>";
        </tr>
      <?php
      }
      ?>
    </tbody>
  <?php
  } else {
    echo "0 results";
  }
  ?>
</table>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...