Delete record by ID in PHP

Asked

Viewed 3,555 times

-1

Good night, you guys! I’m having a GIANT difficulty to delete a BD line by PHP, I imagined the following by the code below: the $line would receive the array with all the BD records,and from it delete the ID I wanted, as it is inside a while I could delete only that ID. But the problem is that it deletes all the lines from the database, can anyone point out to me the error? I imagine it’s with while, but I’m not getting it. If you have some other way to delete I’d also appreciate if you comment, the code goes below:

    <div class="row panel" style="margin-top:1%;">
    <div class="medium-12 columns">
    <table>
        <h1>Jogos Cadastrados</h1>
        <tr>
            <td>ID</td>
            <td>Nome</td>
            <td>Descrição</td>
            <td>Preço</td>
            <td>Excluir</td>
        </tr>
                <?php
        require("connect.php");
        $sql = "select * from tblgames";
        $qry = mysqli_query($con,$sql);
        while($linha = mysqli_fetch_array($qry)){
    ?>
        <tr>
            <td><?php echo $linha["id_game"]?></td>
            <td><?php echo $linha["nome_game"]?></td>
            <td><?php echo $linha["desc_game"]?></td>
            <td><?php echo "R$".$linha["preco_game"]?></td>
            <td><form method="post"><button type="submit"><input type="hidden" name="excluir">&#10005;</button></form>
            <?php 

if(isset($_POST['excluir'])){
    $id = $linha["id_game"];
    $sql2     = "delete from tblgames where id_game='$id'";
    $qry2     = mysqli_query($con,$sql2);
}
?>
            </td>
        </tr>
         <?php } ?>
    </table>
    </div>
    </div>
  • The error is in logic, I do not know how to explain, what you want is different from what is written in the code. The while is a loop, so everything inside will run until something occurs (in this case, until there are no more lines). The $id (inserted in the where id_game = is based on id_game of the current line, so it will delete everything). The first time it displays the td and then delete the line, then repeat delete the second, and then... Fourth... Top....

2 answers

1

Actually the error is logical.

Your while will go through all received data, and will go through all of them, soon it will run delete on all as well and when you do the test

if(isset($_POST['excluir'])){

once the post has been sent with Hidden, all lines will be deleted.

Try this way

    <tr>
            <td><?php echo $linha["id_game"]?></td>
            <td><?php echo $linha["nome_game"]?></td>
            <td><?php echo $linha["desc_game"]?></td>
            <td><?php echo "R$".$linha["preco_game"]?></td>
            <td>
                <form method="post">
                  <input type="hidden" name="id" value=<?php $linha["id_game"]?>>
                  <button type="submit">
                  <input type="hidden" name="excluir">&#10005;</button></form>
            <?php 
if(isset($_POST['excluir'])){
    $id = $_POST['id']);
    $sql2     = "delete from tblgames where id_game='$id'";
    $qry2     = mysqli_query($con,$sql2);
}
  • Thank you so much for your help, but unfortunately I couldn’t by the way you put it. Just to understand, you would have made two types "Hidden", the "delete" to perform the action and the "id" to contain the correct game ID, right?

  • Yes, the delete would only use to see if the desired action is to delete, if so, would use id to remove the table row, and also would not put inside the while this check, otherwise it will compare to each loop repetition, would put before, then the select would already come without the game excluded.

0

The $id = $linha["id_game"]; is based on the current line, which comes from the select * from tblgames, selects all columns of all rows of table tblgames.

So when there is the $_POST['excluir'] it takes the current line, deletes, goes next and deletes...


If you want to delete a specific data you can add the value at the excluir, thus:

<form method="post"><button type="submit"><input type="hidden" name="excluir" value="<?= $linha["id_game"] ?>">&#10005;</button></form>

Now the $_POST['excluir'] shall carry with it the value of the id_game.


Then remove the if(isset($_POST['excluir'])){ loop, and use:

if(isset($_POST['excluir']) && filter_input(INPUT_POST, 'excluir', FILTER_VALIDATE_INT) !== false){

    $id = mysqli_real_escape_string($con, $_POST['excluir']);

    $sql2     = "delete from tblgames where id_game='$id'";
    $qry2     = mysqli_query($con,$sql2);

}

This will cause if there is a POST of excluir having number will perform the deletion of the line that was sent. It is also used the mysqli_real_escape_string only for greater security, although it may be redundant to use it here.


In the end you’ll have something like this:

<div class="row panel" style="margin-top:1%;">
<div class="medium-12 columns">
<table>
    <h1>Jogos Cadastrados</h1>
    <tr>
        <td>ID</td>
        <td>Nome</td>
        <td>Descrição</td>
        <td>Preço</td>
        <td>Excluir</td>
    </tr>
            <?php
    require("connect.php");
    $sql = "select * from tblgames";
    $qry = mysqli_query($con,$sql);
    while($linha = mysqli_fetch_array($qry)){
?>
    <tr>
        <td><?php echo $linha["id_game"]?></td>
        <td><?php echo $linha["nome_game"]?></td>
        <td><?php echo $linha["desc_game"]?></td>
        <td><?php echo "R$".$linha["preco_game"]?></td>
        <td><form method="post"><button type="submit"><input type="hidden" name="excluir" value="<?= $linha["id_game"] ?>">&#10005;</button></form></td>
    </tr>
     <?php } ?>
</table>
</div>
</div>

<?php

if(isset($_POST['excluir']) && filter_input(INPUT_POST, 'excluir', FILTER_VALIDATE_INT) !== false){

    $id = mysqli_real_escape_string($con, $_POST['excluir']);

    $sql2     = "delete from tblgames where id_game='$id'";
    $qry2     = mysqli_query($con,$sql2);

}

?>
  • Thanks for the answer! but unfortunately it didn’t work, I’ll go over here and see if it’s some error of mine writing. Sorry for the ignorance, but what are the commands //mysqli_real_escape_string and //filter_input? I hadn’t seen them before. Ah, and thanks for the explanation of the error, I had understood differently, that I would only get the ID I was clicking the button

  • I corrected an error, which was in case the id was 0, the filter_input filters to check, IN THIS CASE, if it is truly a number, rather than for example "ABC". The mysqli_real_escape_string to prevent/hinder Mysql Injection.

  • When clicking the button the page gives refresh but continue the data in the comic. I put an echo $id to see if he was pulling the amount but he hasn’t written anything, have any idea what it might be?

  • You made the modifications

  • Yes, I made them, want to post as it was? Now I noticed that you edited once again, I will test!

  • Unfortunately it still does not work, it does not give error but does not work the action

  • I didn’t see you insert the connect.php in the middle of the code, you tried to move it to the end, see now with this edition.

  • I had done it, the problem seems is that it does not pull the value up, I do not know why :(

Show 3 more comments

Browser other questions tagged

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