How to Delete record to MySql from PHP using jQuery AJAX

In this article we will learn how to delete a record from the MySQL database using Ajax jQuery with out page refreshing with confirmation box in the PHP.

Now I am explaining how to delete a record or data with out refreshing page using AJAX jQuery and PHP MySQL.
Deleting a data or record using AJAX jQuery is a very simple process.As we are going to discuss that we need database from where we delete a record without refreshing a page. Let us see database details for it.

Creating the Database Table

The first step in this tutorial is the creation of a MySQL database. you can create tables by running SQL queries. Create a table ‘users’ in database using the following SQL query.

This will create a new table ‘users’ in the database. I will use this table to insert file.

 -- Table structure for table `users`  
 CREATE TABLE `users` (  
  `name` varchar(50) NOT NULL,  
  `email` varchar(50) NOT NULL,  
  `password` varchar(50) NOT NULL  

You also need to insert some values in your table

Create MySql Connection in PHP

For importing and exporting database in MySql will make a separate file ‘dbCongif.php’. Add the following code and replace the database credentials with yours. You can find your db credentials in Application Access details:


      // create database connectivity  
      $servername = "localhost";  
      $username = "root";  
      $password = "";  
      $database = "testing";  
      // Create connection  
      $con = new mysqli($servername, $username, $password, $database);  
      // Check connection  
      if ($con->connect_error) {  
            die("Connection failed: " . $con->connect_error);  

Creating HTML Page

  • Now we will create a simple index page to show users table records after inserting in users table by fetching value from database.
  • You can use the following HTML code to create your page. Copy and save it as index.php in your project folder.


<!DOCTYPE html>   
  <html lang="en">   
  <title>How to delete data to MySql from PHP using jQuery AJAX</title>   
  <meta charset="utf-8">   
  <meta name="viewport" content="width=device-width, initial-scale=1">   
  <link rel="stylesheet" href="">   
  <script src=""></script>   
  <script src=""></script>   
    <div class="container">   
     <h2 style="text-align: center;">How to Delete data to MySql from PHP using jQuery AJAX</h2>   
       <div id="tableData">   

Creating jQuery AJAX Script

// Delete record to MySql from PHP using jQuery AJAX  
  if (confirm("Are you sure delete this ?")) {  
    var id = $(this).data('id');  
    var element = this;  
      url :"delete.php",  
        if (data == 1) {  
          alert("User record deleted successfully");       
          alert("Invalid User id");  


      // create database connectivity  
      if (isset($_POST['deleteId'])) {  
           $deleteId = $_POST['deleteId'];  
      // check record exists  
      $sql = "SELECT * FROM users WHERE id = {$deleteId}";  
      $result = $con->query($sql);  
      if ($result->num_rows > 0) {  
           // Delete record from users table  
           $query = "DELETE FROM users WHERE id = {$deleteId}";  
           if ($con->query($query)) {  
                echo 1;  
                echo 0;  

This post is about how to use AJAX to delete record from a database in PHP. If you have any confusion in my code or a problem with the steps, don\’t hesitate to ask in your comments. thank you

You can always support by sharing on social media or recommending my blog to your friends and colleagues.  If you have any suggestions or problems about this tutorial, please comment on the form below.😊

How to Delete record to MySql from PHP using jQuery AJAX


Leave a Reply

Your email address will not be published. Required fields are marked *