Delete Data Using PHP/MYSQL With Javascript Confirmation

0
125

Hello. Good Day! Everyone! Today I’m gonna teach you on creating a simple idea on Delete Data Using PHP/MYSQL With Javascript Confirmation.

As what you read. This not the uncommon tutorial for deleting data. I add twist in my tutorial. So now, you can add a confirmation dialog which would answerable by either “yes” or “no“.

So now let’s start our tutorial.

So first create a database, name it as any name you desire. In my case, I user “itsourcecode” as the name of my database.

Create a table “names” then put the following attributes.

  1. CREATE TABLE <code>names</code> ( <code>name_id</code> int(11) NOT NULL, <code>full_name</code> text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now for the php connection to the database. Create a “connection.php” file then put the following codes.

  1. &lt;?php
  2. $mysqli = new mysqli('localhost', 'root', '', 'itsourcecode');
  3. ?&gt;

On the index page. Just put the following codes.

  1. &lt;?php include 'connection.php'; ?&gt;
  2. &lt;!DOCTYPE html&gt;
  3. &lt;html&gt;
  4. &lt;head&gt;
  5. &lt;meta charset="UTF-8"&gt;
  6. &lt;title&gt;Insert and Delete Data in PHP/MYSQL&lt;/title&gt;
  7.  
  8. function confirmDelete(id) {
  9. if (confirm("Are you sure you want to delete this data?")) {
  10. window.location.href = "delete.php?id=" + id;
  11. } else {
  12. return false;
  13. }
  14. }
  15.  
  16. &lt;/head&gt;
  17. &lt;body&gt;
  18. &lt;h3&gt;Insert and Delete Data in PHP/MYSQL&lt;/h3&gt;
  19. &lt;form method="post" action="insert.php"&gt;
  20. &lt;label&gt;Enter fullname here:&lt;/label&gt;&lt;br&gt;
  21. &lt;input type="text" name="fullname" /&gt;&lt;br&gt;
  22. &lt;input type="submit" name="enter_fullname" value="Add" /&gt;
  23. &lt;/form&gt;
  24. &lt;br&gt;
  25. &lt;?php
  26. if (isset($_GET['delete_action'])) {
  27. if ($_GET['delete_action'] == "success") {
  28. echo 'Successfully Deleted!&lt;br&gt;&lt;br&gt;';
  29. }
  30. }
  31. ?&gt;
  32. &lt;?php
  33. $names = $mysqli-&gt;query("SELECT * FROM names");
  34. while ($data = $names-&gt;fetch_assoc()) { ?&gt;
  35. &lt;b&gt;#&lt;?php echo $data['name_id'] ?&gt;&lt;/b&gt; &lt;?php echo $data['full_name'] ?&gt; | &lt;button onclick="confirmDelete(&lt;?php echo $data['name_id'] ?&gt;);"&gt;Delete&lt;/button&gt;&lt;br&gt;
  36. &lt;?php }
  37. ?&gt;
  38. &lt;/body&gt;
  39. &lt;/html&gt;

Now, for inserting the data first before deleting. Of course, there should be a data to be inserted first.

Create a “insert.php” file then put the following codes.

  1. &lt;?php
  2. include 'connection.php';
  3. if (isset($_POST['enter_fullname'])) {
  4. $fullname = $_POST['fullname'];
  5. $insert = $mysqli-&gt;query("INSERT INTO names (full_name) VALUES ('$fullname')");
  6. if ($insert) {
  7. header("Location: index.php");
  8. } else {
  9. echo $mysqli-&gt;error;
  10. }
  11. }
  12. ?&gt;

Now for deleting the data. Create a “delete.php” file then put the following codes.

  1. &lt;?php
  2. include 'connection.php';
  3. if (isset($_GET['id'])) {
  4. $id = $_GET['id'];
  5. $delete = $mysqli-&gt;query("DELETE FROM names WHERE name_id = $id");
  6. if ($delete) {
  7. header("Location: index.php?delete_action=success");
  8. } else {
  9. echo $mysqli-&gt;error;
  10. }
  11. }
  12. ?&gt;

Screenshots:

If you have questions regarding the tutorial about Delete Data Using PHP/MYSQL With Javascript Confirmation Feel free to ask us by commenting below or by visiting or our contact page. Thank you.

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here