Jump to content
Sign in to follow this  
Flaviaac

Delete registry from DB

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!!

 

 

Share this post


Link to post
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>

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...