Delete record by PHP ID

-1

Good evening, guys! I'm having a GIANT difficulty to delete a DB line from PHP, I figured the following by the code below: $ line would get the array with all the BD records, and from it delete the ID I wanted, how it's in of a while I would be able to delete only that ID. But the problem is that it deletes all the rows from the database, can anyone point me the error? I guess it's with the while, but I can not. If you have any other way to delete it would also appreciate if you commented, the code follows 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>
    
asked by anonymous 03.03.2017 / 23:33

2 answers

1

Actually the error is of logic.

Your while will go through all the data received, and will go through all of them, then will execute the delete in all also 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);
}
    
03.03.2017 / 23:47
0

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

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

If you want to delete a specific data you can add value to excluir , like this:

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

Now $_POST['excluir'] will have its value of id_game with it.

Then, remove the if(isset($_POST['excluir'])){ from the 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 that if there is a POST of excluir owning number will perform deletion of the line that was sent. It also uses mysqli_real_escape_string only for greater security, although it may be redundant to use it here .

In the end it will look 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);

}

?>
    
03.03.2017 / 23:57