Ask before deleting bank data

2

I have this code:

<?php             
if ($result = $mysqli->query("SELECT * FROM usuario ORDER BY id"))
{
    if ($result->num_rows > 0)
    {
        echo "<table border='1' cellpadding='5' cellspacing=0 style=border-collapse: collapse bordercolor='#41c88c'>";

        // definir cabeçalhos de tabela
        echo "<tr> <th>ID</th> <th>Nome</th> <th>Email</th></tr>";

        while ($row = $result->fetch_object())
        {
            // cria uma linha para cada registro
            echo "<tr>";
            echo "<td>" . $row->id . "</td>";
            echo "<td>" . $row->nome . "</td>";
            echo "<td>" . $row->email . "</td>";
            echo "<td><a href='edit_usu.php?id=" . $row->id . "'>Editar</a></td>";
            echo "<td><a href='delete.php?id=" . $row->id . "'>Deletar</a></td>";
            echo "</tr>";
        }

        echo "</table>";
    }
}
?>

And I need a text box to ask first if I really want to delete the data, what is the best way to do this? grateful.

I tried to use it this way:

<script type="text/JavaScript">
    function delete(){
        var agree=confirm("deseja deletar os dados??");
        if (agree)
            return true ;
        else
            return false ;
    }
</script>

But I do not know how to use the variable $ confirm

    
asked by anonymous 23.09.2014 / 20:22

3 answers

6

The simplest way to do this is to use JavaScript, using the confirm and the onclick event:

<a href="http://google.com.br" onclick="return confirm('Deseja mesmo acessar o Google?');">Ir para o Google!</a>

You can see this working in this example: link

Below you can see what your code looks like:

echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->nome . "</td>";
echo "<td>" . $row->email . "</td>";
echo "<td><a href='edit_usu.php?id=" . $row->id . "'>Editar</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "' onclick=\"return confirm('Tem certeza que deseja deletar esse registro?');\">Deletar</a></td>";
echo "</tr>";
    
23.09.2014 / 20:51
2

Test this code so it does exactly what you want ... if you choose cancel it simply for the script and does not give refresh on the page, if it gives a Ok , it part to delete.php with captioning id by GET and do whatever you want according to the instructions in the script in progress, in the delete.php case.

if ($result = $mysqli->query("SELECT * FROM dados ORDER BY id"))
{

        if ($result->num_rows > 0)
        {

            echo "<table border='1' cellpadding='5' cellspacing=0 style=border-collapse: collapse bordercolor='#41c88c'>";


                // definir cabeçalhos de tabela
               echo "<tr> <th>ID</th> <th>Nome</th> <th>Email</th></tr>";

                while ($row = $result->fetch_object())
                {
                // cria uma linha para cada registro
                echo "<tr>";
                echo "<td>" . $row->id . "</td>";
                echo "<td>" . $row->nome . "</td>";
                echo "<td>" . $row->email . "</td>";
                echo "<td><a href='edit_usu.php?id=" . $row->id . "'>Editar</a></td>";
                echo "<td><a href='delete.php?id=" . $row->id . "' onclick=\"return confirm('Tem certeza que deseja deletar esse registro?'); return false;\">Deletar</a></td>";
                echo "</tr>";
                }

            echo "</table>";

         }

}
    
23.09.2014 / 22:19
0

I do not understand why I still insist on passing the id via GET over a link. If you do not understand that the link will appear full for the user with the id at the end, then if he wants to do the "bad" with his DB, he can change the id and give dele deleting them just to make fun of you. Make a submit via POST by typing a pop up or even a new page where there you configure a confirm with another submit that will lead to the file that denounces by require, in this way this file will not be exposed nor the id, making it impossible anyone who has the delete link simply causes them major headaches.

    
14.12.2016 / 03:45