Delete via php

Asked

Viewed 229 times

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>

And there were those mistakes here:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 2

    Connection is done only once at the top of the page. No need to re-call the file. td with LINK to delete.

  • when Voce speaks link will be a href

  • Exactly. <a href="deletar.php?cod=<?php echo $row['id']?"> Deletar </a>

  • @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.

1 answer

2

You can create that 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 function or are working with some framework, but at the bottom, that would be... I believe you must resolve your question and doubt.

  • friend edited my question and added that you said more of a one-look mistake

  • 1

    in the second line, try to do the following: if($Conn){ echo "connected"; } Else { echo "without connection"; }

  • vc says on the suggested line where you have that condition if(isset($_GET['delete'])): or $Conn = mysqli_connect ($varBDhostname,$varBDusername,$varBDpassword,$varBDdatabase);

  • can put before this if(isset($_GET['delete'])): , put: if($Conn){ echo "connected"; } Else { echo "without connection"; } -

  • put and nothing friend

  • But it appears connected?

  • does not appear not friend I looked well here and does not appear

  • ok friend I’ve already logged in there on chat[

Show 4 more comments

Browser other questions tagged

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