How To Use SQL DELETE Statement In Query Using PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

To remove one or more rows from a table, you use the DELETE statement.SQL DELETE Query is used to delete the existing records from a table where to delete the specified values we need to use where clause.

SQL DELETE Statement

SQL DELETE Statement

The DELETE Statement in SQL is used to delete existing records from a table. We can delete a single record or multiple records depending on the condition we specify in the WHERE clause.


DELETE Syntax and Usage:

DELETE FROM table_name WHERE condition;

Deleting the specified data from the table.

Let's create.

1.create database and table.
   Student table
StudentID StudentName ParentsContact Address Country DOB Section

Branch


  1
    Liam 10101010 23 MG road
Banglore  
INDIA 12/06 A CSE 
  2    Andrew 20202020 2415 Anthony  market str Germany 01/02 A ISE 
  3    Joshua 30303030 26547 David church road USA 18/04 B CL 
 4     Ryan 40404040 18 Krishna sq.
Banglore
INDIA 26/12 B EE
 5    Charles 50505050 878 Eli Berguvsvägen 8 INDIA 23/11 C ECE

DELETE Statement Example :
$sql = "DELETE FROM Student WHERE StudentName='Juan'";

above example delete studentname who is Juan from the table student.We can delete single as well as multiple records depending on the condition we provide in WHERE clause. If we omit the WHERE clause then all of the records will be deleted and the table will be empty.

Complete code of SQL DELETE statement in PHP script.
<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to  use SQL DELETE Statement</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>SQL DELETE Statement</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");
        if($connect === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }

        $sql = "DELETE FROM Student WHERE StudentName='Juan'";
        if(mysqli_query($connect, $sql)){
            echo "<h1>Deleted Record successfully.</h1>";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($connect);
        }
        mysqli_close($connect);
        ?>
    </div>
    <br>
</div>
</body>
</html>
  
 

Related Post