PHP MySQL Delete


The DELETE FROM statement is used to delete rows from a database table. The following example creates and populates a Members table using the following SQL commands:

CREATE TABLE Members (
  ID   INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(16) NOT NULL );
   
INSERT INTO Members( name ) VALUES( '$name1' );

and deletes the items using:

DELETE FROM Members WHERE name LIKE '%$name2%';

           
 <html><body>
 <?php
  $servername = "undcsmysql.mysql.database.azure.com";
  $username   = "user.id@undcsmysql";
  $password   = "password";
  $dbname     = "user_id";         # your database or schema
  // Connect to the database.
  $conn       = new mysqli( $servername, $username, $password, $dbname );

  $action =   Drop    Create    Insert ( name = "" ) 
              Delete ( WHERE name LIKE '')               
  if ( $action == "Drop" ) {
    // Drop table Members.
    $conn->query( "DROP TABLE Members" );
    echo "Table Members dropped";
  }
  elseif ( $action == "Create" ) {
    // Create table Members.
    $sql = "CREATE TABLE Members (
              ID   INT AUTO_INCREMENT PRIMARY KEY,
              name VARCHAR(16) NOT NULL )";
    $conn->query( $sql );
    echo "Table Members created";
  }
  elseif ( $action == "Insert" ) {
    // Insert member.
    $sql = "INSERT INTO Members( name ) VALUES( '$name1' )";
    $conn->query( $sql );
    echo "Member $name1 entered"
  }
  elseif ( $action == "Delete" ) {
    // Delete members.
    $sql = "DELETE FROM Members WHERE name LIKE '%$name2%'";
    $conn->query( $sql );

    // Select all.
    $result = $conn->query( "SELECT * FROM Members" );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['ID'] . ", " . $row['name'];
  }
  $conn->close( );
 ?>
 </body></html>