SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

PHP | MySQL Delete Query

If you want to delete records from a MySQL table using PHP, you'd typically use the DELETE SQL statement.

Here's a simple step-by-step example:

1. Connect to the MySQL database:

$servername = "your_server_name";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

2. Write and Execute the DELETE Query:

Suppose you have a table named users and you want to delete a user with the id of 5:

$sql = "DELETE FROM users WHERE id=5";

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

3. Close the connection:

$conn->close();

Remember: Be very careful when using the DELETE statement. Without a WHERE clause, it will remove all records from the table. Always ensure you're deleting the right records to avoid unintentional data loss.

Also, for better security, consider using prepared statements to prevent SQL injection attacks.

  1. PHP MySQL DELETE query example:

    • Use a simple PHP MySQL DELETE query to remove records from a table.
    <?php
    $conn = new mysqli("localhost", "your_username", "your_password", "your_database");
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "DELETE FROM your_table WHERE id = 1";
    
    if ($conn->query($sql) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . $conn->error;
    }
    
    $conn->close();
    ?>
    
  2. How to delete records in MySQL using PHP:

    • Delete records in MySQL using PHP.
    <?php
    $conn = new mysqli("localhost", "your_username", "your_password", "your_database");
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "DELETE FROM your_table WHERE id = 1";
    
    if ($conn->query($sql) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . $conn->error;
    }
    
    $conn->close();
    ?>
    
  3. Deleting specific records with WHERE clause in PHP:

    • Use the WHERE clause in a PHP MySQL DELETE query to delete specific records.
    $id_to_delete = 1;
    $sql = "DELETE FROM your_table WHERE id = $id_to_delete";
    
  4. PHP MySQL DELETE multiple rows:

    • Delete multiple rows in MySQL using a PHP MySQL DELETE query.
    $ids_to_delete = [1, 2, 3];
    $id_list = implode(", ", $ids_to_delete);
    $sql = "DELETE FROM your_table WHERE id IN ($id_list)";
    
  5. Deleting records with user input in PHP and MySQL:

    • Allow users to input values to delete specific records.
    $user_input = $_POST['delete_value'];
    $sql = "DELETE FROM your_table WHERE column_name = '$user_input'";
    
  6. Confirming deletion with a prompt in PHP:

    • Confirm deletions with a prompt in PHP before executing the MySQL DELETE query.
    // Add a confirmation prompt in your HTML/JS
    // Example: <button onclick="confirmDelete()">Delete</button>
    
    function confirmDelete() {
        if (confirm("Are you sure you want to delete this record?")) {
            // Proceed with the deletion
            // Make an AJAX call or submit a form to trigger the PHP script
        }
    }
    
  7. Using DELETE query with JOIN in PHP:

    • Use a PHP MySQL DELETE query with JOIN to delete records from multiple tables.
    $sql = "DELETE your_table, related_table FROM your_table
            LEFT JOIN related_table ON your_table.id = related_table.your_table_id
            WHERE your_table.id = 1";
    
  8. Deleting records based on date in PHP and MySQL:

    • Delete records based on date criteria using PHP and MySQL.
    $date_to_delete = "2023-01-01";
    $sql = "DELETE FROM your_table WHERE date_column <= '$date_to_delete'";
    
  9. Handling foreign key constraints with DELETE in PHP:

    • Handle foreign key constraints when deleting records in PHP and MySQL.
    $conn->query("SET foreign_key_checks = 0");
    $sql = "DELETE FROM your_table WHERE id = 1";
    $conn->query("SET foreign_key_checks = 1");
    
  10. PHP MySQL DELETE query for soft deletes:

    • Implement soft deletes using a PHP MySQL DELETE query by updating a "status" column.
    $sql = "UPDATE your_table SET status = 'deleted' WHERE id = 1";
    
  11. Deleting records with AJAX and PHP:

    • Delete records asynchronously using AJAX and PHP.
    // PHP script (delete_record.php)
    <?php
    $id_to_delete = $_POST['id'];
    $sql = "DELETE FROM your_table WHERE id = $id_to_delete";
    // Execute the query and return a response
    ?>
    
    // JavaScript (using AJAX)
    function deleteRecord(id) {
        if (confirm("Are you sure you want to delete this record?")) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    // Handle the response, e.g., update UI
                }
            };
            xhr.open("POST", "delete_record.php", true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send("id=" + id);
        }
    }
    
  12. PHP DELETE query error handling:

    • Handle errors that may occur during the execution of a PHP MySQL DELETE query.
    if ($conn->query($sql) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . $conn->error;
    }