Delete Data Using PHP/MYSQL With Javascript Confirmation

0
87

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 `names` ( `name_id` int(11) NOT NULL, `full_name` 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. <?php
  2. $mysqli = new mysqli('localhost', 'root', '', 'itsourcecode');
  3. ?>

On the index page. Just put the following codes.

  1. <?php include 'connection.php'; ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Insert and Delete Data in PHP/MYSQL</title>
  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. </head>
  17. <body>
  18. <h3>Insert and Delete Data in PHP/MYSQL</h3>
  19. <form method="post" action="insert.php">
  20. <label>Enter fullname here:</label><br>
  21. <input type="text" name="fullname" /><br>
  22. <input type="submit" name="enter_fullname" value="Add" />
  23. </form>
  24. <br>
  25. <?php
  26. if (isset($_GET['delete_action'])) {
  27. if ($_GET['delete_action'] == "success") {
  28. echo 'Successfully Deleted!<br><br>';
  29. }
  30. }
  31. ?>
  32. <?php
  33. $names = $mysqli->query("SELECT * FROM names");
  34. while ($data = $names->fetch_assoc()) { ?>
  35. <b>#<?php echo $data['name_id'] ?></b> <?php echo $data['full_name'] ?> | <button onclick="confirmDelete(<?php echo $data['name_id'] ?>);">Delete</button><br>
  36. <?php }
  37. ?>
  38. </body>
  39. </html>

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

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

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

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
(Visited 88 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here