3
I am programming a page in php and I am with the following doubt I made a Insert in my database and created a <table>
the inside of it are being displayed my data that would be the code:
<table id="tb1" class="display" cellspacing="0">
<thead>
<tr>
<th>id</th>
<th>Produto</th>
<th>valor</th>
<th>data</th>
<th>apagar</th>
</tr>
</thead>
<tbody id="tbody">
<?php
include ('conection/config.php');
$conn = mysqli_connect ($varBDhostname,$varBDusername,$varBDpassword,$varBDdatabase);
$result = mysqli_query($conn,'SELECT *FROM tbl_produtos') or die(mysqli_error());
while ($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row["id"]?></td>
<td><?php echo $row["nome"]?></td>
<td><?php echo $row["valor"]?></td>
<td><?php echo $row["dh"]?></td>
</tr>
<?php } ?>
</tbody>
</table>
my connections are working normally however I would need to put a column that contains a delete button only my doubt and the next one I should make a new connection because I realized my tag <tr>
is inside while doing select so I don’t know where the delete would be and I should take advantage of the same table at least is what I think could do these write
I made this delete and here:
<header class="header">
<form action="conection/inserir.php" method="post" id="form">
<h1>Inserir Produtos</h1>
<input type="text" name="nome" id="produto" placeholder="Produto" maxlength="30" required/>
<input type="number" name="valor" id="valor" placeholder="valor" maxlength="10" required/>
<input type="submit" value="cadastrar" name="cadastro">
</form>
<?
include ('conection/config.php');
$conn = mysqli_connect ($varBDhostname,$varBDusername,$varBDpassword,$varBDdatabase);
if(isset($_GET['deletar'])):
$id = $_GET['deletar'];
$delete = mysqli_query($conn,'DELETE FROM tbl_produtos WHERE id = {$id}') or die(mysqli_error());
if($delete):
echo "Registro removido";
endif;
endif;
?>
<table id="tb1" class="display" cellspacing="0">
<thead>
<tr>
<th>id</th>
<th>Produto</th>
<th>valor</th>
<th>data</th>
<th>apagar</th>
</tr>
</thead>
<tbody id="tbody">
<?php
$result = mysqli_query($conn,'SELECT *FROM tbl_produtos') or die(mysqli_error());
while ($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row["id"]?></td>
<td><?php echo $row["nome"]?></td>
<td><?php echo $row["valor"]?></td>
<td><?php echo $row["dh"]?></td>
<td><a href="?deletar=<?php echo $row["id"]?>">Deletar</a></td>
</tr>
<?php } ?>
</tbody>
</table>
Connection is done only once at the top of the page. No need to re-call the file.
td
with LINK to delete.– Diego Souza
when Voce speaks link will be a href
– Felipe Henrique
Exactly.
<a href="deletar.php?cod=<?php echo $row['id']?"> Deletar </a>
– Diego Souza
@Kirito I posted an answer. Note that I entered the connection at the top of the file and drew up a href so that you can find the registry ID and delete it as the id passed.
– Sr. André Baill