-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">✕</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
whileis a loop, so everything inside will run until something occurs (in this case, until there are no more lines). The$id(inserted in thewhere id_game =is based onid_gameof the current line, so it will delete everything). The first time it displays thetdand then delete the line, then repeat delete the second, and then... Fourth... Top....– Inkeliz