-1

I have a button which deletes a MySQL database. It works great, but I just would like to have a confirmation before doing so.

button HTML

<form action="delete.php" method="post">
    <input type="submit" name="someAction" value="Delete all database" />
</form>

delete.php

<?php
$servername = "server";
$username = "user";
$password = "";
$dbname = "testdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// sql to delete a record
$sql = "DELETE FROM exampledb";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Antonio Andrés
  • 169
  • 2
  • 15
  • Use a button type="button" and capture onclick events with javascript, prompting a confirmation modal or alert to the user. If he accepts, then submit the form through JS. – Dleep Sep 05 '18 at 18:47
  • you realize that you'll be deleting your entire database and is unknown if you want to do that or something in particular. – Funk Forty Niner Sep 05 '18 at 18:51
  • @FunkFortyNiner The button does say "Delete all database". So... yeah, it does seem to be intended. Odd, but intended. – Patrick Q Sep 05 '18 at 18:53
  • @PatrickQ Ah, yes of course. Thanks for pointing that out to me. Slight bit of a "duh" on my part :-)) – Funk Forty Niner Sep 05 '18 at 18:53
  • @PatrickQ Yes, a bit odd... I am starting to learn PHP using simple functions and understanding its processes. Next step is deleting just every row. Hopefully I will make it! Thanks for your help. – Antonio Andrés Sep 05 '18 at 19:33
  • @Dleep thanks! I will – Antonio Andrés Sep 05 '18 at 19:34

1 Answers1

0

you can use the confirm() function ....

 <form action="delete.php" method="post"  onSubmit="return confirm('Are you sure to delete?')">
     <input type="submit" name="someAction" value="Delete all database" />
 </form>