Deleting records from database using php and mysql

Deleting records from database using php is quite easy. You have already learn how to select and display records from database, after selecting and displaying the records you just need to have an additional column to delete the records, please see the image below


Note:- You cannot delete any single record, deletion process occurs for whole row.

As you see in above image, whenever you will put the cursor of your mouse on the hyperlink (Delete) the u will see a link with query string on the status bar, this is the best method to delete the records.

Note:- There must be unique query string for every row to delete it so that when u try to delete any specific row then only that row should be deleted.

Here is the code to develop that above page



Explaination:-
As you can see on above page that it is 99% same page as we have for selecting and displaying records page, yeah off course but one thing that is additional in this page is Delete column that i also highlight in the above snapshot of the code. Now let's understand that highlight portion

I have made a column to Delete the row, for this i use caption Delete and make it hyperlink to that page where deletion of records code is written, i pass a querystring to that page also with the help of a variable rollno, rollno is a unique key in my senario that's why i use it to delete rows.

Now let's check the code that performs whole deletion process


Explaination:-

require_once('conn.php');
$rollno=$_GET['rollno'];
$delete_query="delete from student where rollno='$rollno'";
$resultset=mysql_query($delete_query);
if($resultset)
{
header("location:display.php");
}
else{
mysql_error();
}
?>
first of all we include connection file to our php script then we get the query string value using $_GET[], query string always get using $_GET[''], after getting the query string we store its value in a variable $rollno, then we write and execute query to delete the specific row, after this we perform a check and that check is if query successfully executed then redirect to the page where all records are displaying so that we can confirm the deletion process else we are showing errors using mysql_error(); function.

So this is the way to delete the records or delete the rows using php and mysql.

0 comments: