PHP deleting data from MySQL database

      No Comments on PHP deleting data from MySQL database

PHP deleting data from MySQL database -delete command

Learn about SQL delete command. Please refer to the post.

PHP deleting data from MySQL database

Creating a form

  1. Form is enclosed in <form> </form> tags.
  2. Specify the method attribute as GET or POST depending on the data being sent to the php script
  3. Set the action attribute with the URL of the php script eg:request-handler.php
  4. NOTE: Each input/select/textarea field in the form should contain name attribute. Without name attribute the value submitted will not be sent to the script.
  5. A sample form

    <form method="POST" action="request-handler.php">
      <label>Enter your email:</label>
      <input type="email" name="email" placeholder="Enter Email" required/>
      <label>Enter new password:</label>
      <input type="password" name="password" placeholder="New Password" required/>
      <input type="submit" value="login"/>

    Demo database:

    PHP deleting data from MySQL database

    PHP deleting data from MySQL database – request-handler.php

    Here we will use email value entered by the user to match it to the row where the column containing password.

      if(isset($_POST['submit'])) //avoid error:undefined index
          $email=$_POST['email']; //getting values from the form
      // establishing a database connection
      define("HOST", "localhost");     // The host you want to connect to.
      define("USER", "user_db");    // The database username. 
      define("PASSWORD", "123$%bb^7aqq!2");    // The database password. 
      define("DATABASE", "sample_db");    // The database name.
      $connection = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
      // Check connection
      if (mysqli_connect_errno())
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
      // checking for empty variables
      if($email="" ||$password="")
        echo "Please enter a valid value to each input";
        //building a query
        $sql="DELETE FROM `student` WHERE `stdpassword`='$password' AND `emailid`='$email'";
        //running query
        //success message
            echo "Record deleted successfully";

    The delete query

    This query deletes the record information from the database. where `emailid`='$email' AND `password`='$password'is used to match the row to the email of the user.This deletes the required row.

     $sql="DELETE FROM `student` WHERE `stdpassword`='$password' AND `emailid`='$email'";

    How it is done:

    1. User fills the form with his/her email and the new password.
      PHP deleting data from MySQL database
    2. On clicking the submit button ->request-handler.php script is called.
    3. It runs the delete statement on the database.
       $sql="DELETE FROM `student` WHERE `stdpassword`='iamablog' AND `emailid`=''";
    4. Rows are affectedPHP deleting data from MySQL database
    5. Resulting database is:PHP deleting data from MySQL database

Leave a Reply

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