saftutorials.com
MYSQL DELETE DATA

Create the MySQL query for deleting a row from the table and then execute it with mysql_query() function. We can delete a row from the table by using MYSQL DELETE statement.


Syntax of MYSQL DELETE statement

DELETE FROM table_name WHERE some_column=some_value;


Below is a simple example to delete a record from student table.Run this example for better understanding delete operation.

For deleting a row you have to provide student id.

<?php
 $database_host = 'localhost:3036';
 $database_user = 'root';
 $database_pass = '';
 $conn = mysql_connect($database_host, $database_user, $database_pass);
 if(! $conn ) {
   die('Could not connect: ' . mysql_error());
 }else{
   echo 'Connected successfully\n';
}
 $selected_or_not = mysql_select_db( 'xyz_school' );
 if(! $selected_or_not ) {
   die('Not Selected xyz_school database');
 }else{
   echo 'Selected xyz_school database';
}
 $sql = 'DELETE FROM student WHERE student_id = 1';
 $result = mysql_query( $sql, $conn );
 if(! $result ) {
  die('Could not deleted the row: ' . mysql_error());
 }
echo "Deleted successfully";
 mysql_close($conn);
?>
<!DOCTYPE html>
<html>
<body>
    <h2>Student List</h2>
     <table>
        <th>
           <td>Student ID</td>
           <td>Student Name</td>
           <td>Address</td>
           <td>Enrollment No</td>
           <td>Joining date</td>
           <td>Action</td>
        </th>
<?php
$i = 1;
while($row = mysql_fetch_array($result)) {
?>
        <tr>
           <td><?php echo $row['student_id'];?> </td>
           <td><?php echo $row['student_name'] ."or". $row[1];?> </td>
           <td><?php echo $row['student_address'] ."or". $row[2];?> </td>
           <td><?php echo $row['student_enrollment_no'] ."or". $row[3];?> </td>
           <td><?php echo $row['join_date'] ."or". $row[4];?> </td>
           <td>Del</td>
        </tr>
<?php
}
?>
     </table>
  </body>
</html>

IDStudent NameAddressEnrollment NoJoining DateAction
1JohnCalifornia1010/02/2017Del
2JakobNew York2020/05/2017Del
Now you can link your delete page with Del when Delete successful then page redirects automatically your current page. 

Learn PHP Redirects
Multiple Data Delete
You can delete multiple data from MySQL Database. Just only one step for this. For delete multiple data from database you should not define only one student id like student id = 1, but you should define a value which you want to delete: 
Example 
$sql = 'DELETE FROM student WHERE city = California '; // In this Query delete all your record where city California. So be careful with this operation.




Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic