All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Delete A Row In MySQL Using PHP

Last Updated : Mar 11, 2024

How To Delete A Row In MySQL Using PHP

In this article we will show you the solution of how to delete a row in MySQL using PHP, data can be deleted from MySQL tables by using the PHP function mysql query to execute SQL DELETE statements.

Use the following illustration to remove records from the customer table.

Any table's conditional clause must be used to search for a record that you want to delete.

In the example below, a primary key can be used to match an employee record.

In order to remove only the records from such a database table that meet specific requirements, DELETE queries are typically used in conjunction with "Select" statements.

Step By Step Guide On How To Delete A Row In MySQL Using PHP :-

<html>
   <head>
      <title>TalkersCode Delete a Record from Database</title>
   </head>
   <body>
      <?php
         if(isset($_POST['delete'])) {
            $dbhost = 'localhost:3036';
            $dbuser = 'root';
            $dbpass = 'rootpassword';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);
            if(! $conn ) {
               die('Could not connect: ' . mysql_error());
            }
            $emp_id = $_POST['emp_id'];
            $sql = "DELETE FROM employee WHERE emp_id = $emp_id" ;
            mysql_select_db('test_db');
            $retval = mysql_query( $sql, $conn );
            if(! $retval ) {
               die('Could not delete data: ' . mysql_error());
            }
            echo "Deleted data\n";
            mysql_close($conn);
         }else {
            ?>
               <form method = "post" action = "<?php $_PHP_SELF ?>">
                  <table width = "400" border = "0" cellspacing = "1"
                     cellpadding = "2">
                     <tr>
                        <td width = "100">Employee ID</td>
                        <td><input name = "emp_id" type = "text"
                           id = "emp_id"></td>
                     </tr>
                     <tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>
                     <tr>
                        <td width = "100"> </td>
                        <td>
                           <input name = "delete" type = "submit"
                              id = "delete" value = "Delete">
                        </td>
                     </tr>
                  </table>
               </form>
            <?php
         }
      ?>
   </body>
</html>
  1. The first thing we do is create HTML that informs the browser of the version designers are using.The first thing you notice when viewing an HTML document are the tags.
  2. The header tag will be used to specify the project heading.
  3. A head is used with a <head> , after that uses a closed title, resulting in an open title or a closed title.
  4. A body tag specifies the information that will be included on the webpage. Everything is only published on our website.
  5. ? The brief tagging concludes with >. Short-named tags begin with "?" but also end with "?>". Before using the short style tags, the server's php.ini file must have the short style tags enabled.
  6. After creating the host,username, and password, a connection can be made.
  7. You must check a $_POST variable array to make sure it doesn't have any empty values before using any data that was submitted thru the registration form. The sign up process requires that all form source fields be completed, so no empty values are permitted.
  8. Our next step is to output one or more strings using the echo() function. You do Because echo() is not a function, no parentheses are required.
  9. The following step is to generate a shape using a bar counter format, like table draw but rather table row inside the form.
  10. The code must then be executed after closing the /body> and /html> tags.

Conclusion :-

Just like when records are inserted into tables, SQL DELETE can also be used to remove records from those tables.

This clause is frequently used in the WHERE clause to delete just records which also meet certain criteria.

I hope this article on how to delete a row in MySQL using PHP helps you and the steps and method mentioned above are easy to follow and implement.