Button Delete a line in DB

Asked

Viewed 597 times

2

I’m having trouble with a little code with delete button. I’ve tried a few ways but I couldn’t get the button to $deletar do the delete function.

Follows code show each registered slide and wanted to delete button.

<?php       
        $sql = mysql_query("SELECT * FROM slider");
        while($linha = mysql_fetch_array($sql)){

            $id = $linha['id'];
            $imagem = $linha['imagem'];
            $comentario = $linha['comentario'];
            $link = $linha['link'];
    ?>
  <tr>
    <td><img src="pages/<?= $imagem ?>" width="150px" height="100px"></td>
    <td class="mailbox-name">
      <?= $comentario ?>
    </td>
    <td class="mailbox-subject">
      <?= $link ?>
    </td>

    <td class="mailbox-date">
      <input type="button" class="btn btn-danger" src="<?= $deletar ?>" value="Excluir">
    </td>
  </tr>
  <?php }    ?>     
  • 1

    You’re missing an echo there, <? echo $delete ?>

  • Why src="<?= $deletar ?>" ? Instead of you wearing input you can use the <a href="#" class="btn btn-danger" onclick="chamaDeletar(".<?php echo $deletar; ?>.")"> Excluir</a>

1 answer

1

Your code is wrong, follow it corrected

<?php       
        if(isset($_GET['acao'])){
            $del = mysql_query("DELETE FROM slider WHERE id = '$_GET['id']'")
        }
        $sql = mysql_query("SELECT * FROM slider");
        while($linha = mysql_fetch_array($sql)){

            $id = $linha['id'];
            $imagem = $linha['imagem'];
            $comentario = $linha['comentario'];
            $link = $linha['link'];
    ?>
  <tr>
    <td><img src="pages/<?= $imagem ?>" width="150px" height="100px"></td>
    <td class="mailbox-name">
      <?= $comentario ?>
    </td>
    <td class="mailbox-subject">
      <?= $link ?>
    </td>

    <td class="mailbox-date">
       <a href="id=<?= $deletar; ?>">Deletar</a>
    </td>
  </tr>
  <?php }    ?> 

Recalling that the href that will perform its action, if you want to execute the code on another page (the code that excludes), would be so <a href="pagina.php?acao=deletar&id=<?= $id; ?>">Deletar</a> and so on.

  • But it is the following This script takes everyone who is registered and shows with name, image, comment and link one in each table if in the respective that I clicked and the action= I have to identify but I do not know how to add because I already have a mysql_query pulling the created tables and how I could create inside it so show each one they contain a delete button but being for each one

  • @user84482 will edit the code

  • @user84482 would be more or less the way I put it up, and something else, don’t useMYSQL_, was discontinued. use Mysqli_ or PDO

Browser other questions tagged

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