Deleting Data from within the Database using PHP

    You can delete data from the database using delete command within the mysql_query function.

    Example:

    <html>
       <head>
          <title>Deleting a Record from Database</title>
       </head>
       <body>
    <?php
             if(isset($_POST[‘delete’])) {
       $db_host = 'localhost:3036';
       $db_user = 'root';
       $db_pass = 'rootpassword';
       $db_conn = mysql_connect($db_host, $db_user, $db_pass);   
       if(! $db_conn ) {
          die('Connection failed: ' . mysql_error());
       }
    $stu_id = $_POST['stu_id'];
    $db_sql = "DELETE from student where stu_id = $stu_id" ;
       mysql_select_db('demo_db');
       $db_retval = mysql_query( $db_sql, $db_conn );
    if(! $db_retval ) {
          die('data cannot not be deleted: ' . mysql_error());
       }
    echo "deleted data successfully\n";
       mysql_close($db_conn);
    ?> else
    {
    ?>
    <form method = "post" action = "<?php $_PHP_SELF ?>">
                      <table width = "400" border =" 0" cellspacing = "1" 
                         cellpadding = "2">                  
                         <tr>
                            <td width = "100">Student ID</td>
                            <td><input name = "stu_id" type = "text" 
                               id = "stu_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>

    People are also reading: