PHP Crud Operation - Delete Data In MySQL Using PHP

PHP Crud Operation - Delete Data In MySQL Using PHP

This is the last article of the php crud operation series, in which you will learn the steps to delete a record. In the process of deleting a specific record, the record is deleted based on the unique ID. Like a specific record is edited (updated) based on the unique ID.

php crud operation

Understanding the delete process, when a record has to be deleted, usually the "ID" of our record is sent through a URL. This process happens through an anchor tag (link). In this, you have to create a link that passes the "ID" as a query parameter of the URL. As soon as the user clicks on the link, that ID reaches the server, where the delete operation is performed.

After this, the delete code is written on a separate page or backend script, which reads the ID from the URL and deletes the record from the database. This process ensures that the specific record is deleted safely without any error. Is it efficient to manage delete operations this way, as there is direct communication via URL and user interaction is simple?

php crud operation
Before Delete The Record

Code of the deleted record


                 
    <?php
        require('../connection/conn.php');
        $get_id = $_GET['id'];  
        $delete_data= "DELETE FROM `simple_table` WHERE `id` = $get_id ";
        $delete_data_query = mysqli_query($conn ,$delete_data);
        if($delete_data_query){
            header('location:../index/index.php');
        }
    ?>
                   
Get Source Code
This PHP code is designed to delete a record from the database. First, the conn.php file is included which contains the code for the connection to the database. Including this file allows us to interact with the database.

php crud operation
After Delete The Records

Then, the id value can be retrieved from the URL via $_GET['id']. This id is used to identify the record we need to delete. Delete with SQL query deletes the record with the id from the simple_table.

If the query runs successfully, the header() function requires that the user is redirected to a new page (i.e. index.php). If the query fails, no error message is displayed, the program just remains silent.

Post a Comment

Previous Post Next Post

Recent in Technology