Ask before deleting database data

Asked

Viewed 9,511 times

1

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 before if I really want to delete the data, what’s the best way to do that? grateful.

I tried to use it that way:

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

but I don’t know how to use the variable $confirm

  • 1

    Ah, the name of your function is delete. delete is a reserved word for Javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete. Changes function name (to remove, for example) and everything should work.

  • 2

    delete was just as an example, the variable I used was idlink and what I can’t understand is the syntax error that runs on this line echo "<td><a href='delete.php? id=" . $Row->id . " ' onclick="Return confirm('Are you sure you want to delete this record?');">Delete</a></td>";

  • 1

    I edited my answer. As @People mentioned, double quotes were missing because of echo.

3 answers

5


The simplest way to do this is by using Javascript, through the function confirm and the event onclick:

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

You can see this working in this example: http://jsfiddle.net/tbcww98q/

Below you can see how your code would look:

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>";
  • 1

    i was in doubt how to use confirm, thanks for clearing my doubts, but accuses syntax error, I do not know how to use the function along with Row->id

  • 1

    Why would you need to use $row->id in the confirm?

  • 1

    Have you edit your question and post your new code?

  • 1

    is a table with multiple data, '$Row' searches the id to be able to delete.

  • But in your code you are not using confirm. It has how to post the way you tried to use and error that occurred?

  • 2

    You forgot to escape the double quotes inside the string, the line with the onclick should look like this: echo "<td><a href='delete.php?id=" . $row->id . "' onclick=\"return confirm('Tem certeza que deseja deletar esse registro?');\">Deletar</a></td>";

  • Thanks, I’m all set :)

  • With your help it worked, thank you very much!

Show 3 more comments

1

Test this code like this, it does exactly what you want ... if you choose cancel it simply stops the script and does not give refresh on the page, if you give a Ok, he leaves for delete.php with capture from id for GET and does whatever you want according to the instructions in the current script, in case delete.php.

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>";

         }

}

0

I don’t understand why they still insist on passing the id via GET through a link. Did not realize that the link will appear complete to the user with the id at the end, so if he wants to do the "bad" with his DB, can keep changing the id and give enter deleting the same just to make fun of you. Make a Submit via POST linking a pop up or even a new page where there you configure a confirm with another Submit that will lead up to the file you denounce by require, so this file will not be exposed and neither the id, making it impossible for anyone who has the delete link to simply cause you a severe headache.

Browser other questions tagged

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