Do delete via php

3

I am programming a page in php and I have the following doubt I made an insert in my database and created a <table> a inside it are being displayed my data that would be the code: / p>

 <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 but I would need to put a column that contains a delete button, but my doubt and the next one I should make a new connection because I realized that my tag <tr> is inside the while that makes the select logo no I know where it would be from delete being that I should take advantage of the same table at least is what I think how could do these deletes

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>

And gave these errors here:

    
asked by anonymous 10.08.2015 / 20:32

1 answer

2

You can create it this way:

<?
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 Registro</a></td>
        </tr>
        <?php } ?>
    </tbody>
</table>

Unless you have some deleting or working with some framework, but at the bottom, that would be it ... I think you should resolve your question and question.

    
10.08.2015 / 20:43