Delete record through PHP

SAMPLE: A FEEDBACK SYSTEM


Let’s start by opening the (feedback_pg.php) and introducing the Delete link button after the Edit button. I did this with the bolded line of code below.

<HTML>
<HEAD>
<TITLE> Company Feedback </TITLE>
</HEAD>
<BODY>
<b>FEEDBACK LIST</b>
<br />
<br />
<?php
//include the connection string
include("conn.php");

//the table header
print "
<table width=\"50%\" border=1>
<tr>
<td>NAME</td>
<td>EMAIL</td>
<td>COMMENT</td>
<td>DATE POST</td>
<td> </td>
<td> </td>
</tr>
";

// how many rows to show per page
$rowsPerPage = 3;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
     $pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

//query the table
$query  = "SELECT * FROM feedback ORDER BY id DESC LIMIT $offset, $rowsPerPage";

$result= mysql_query($query, $conn) or die(mysql_error());

if (mysql_num_rows($result) == 0) {
 print "SORRY: no data";
 exit;
}

//loop through the query result and print
while($info = mysql_fetch_array($result)){

     $data_id = $info['id'];
     $data_name = $info['name'];
     $data_email = $info['email'];
     $data_comment = $info['comment'];
     $data_date = $info['date_submit'];

           print "
           <tr>
           <td> $data_name </td>
           <td> $data_email </td>
           <td> $data_comment </td>
           <td> $data_date </td>
           <td> <a href=\"feedback_edit.php?id=$data_id\"> Edit </td>
           <td> <a href=\"feedback_delete.php?id=$data_id\"> Delete </td>
           </tr>
           ";
}
print "</table>";

 

// how many rows we have in database
... The remaining paginating code
?>

</BODY>
</HTML>


Just like we did with Edit we added <td> </td> to the heading, see the first bolded line above, then the Delete link this is done the same way we did the Edit.

<td> <a href=\"feedback_delete.php?id=$data_id\"> Delete </td>

The browser preview will look like what we have below, and the query link will show in the browser status bar when you move our mouse to the link as we did with Edit.


Now create the page feedback_delete.php and let it have the code below.

 

feedback_delete.php

<?php
//include the connection string
include("conn.php");

//To be executed after the confirmation of the delete
if(isset($_GET['final_del'])){
     //get the id from the confirmation link
     $query_id = mysql_real_escape_string($_GET['final_del']);

     //the deleting query
     $query  = "DELETE FROM feedback WHERE id = '$query_id'";
     $result = mysql_query($query) or die(mysql_error());
    
     //redirect back after deleting
     header("Location:feedback_pg.php");
}
?>
<!-- THE HTML SECTION -->
<HTML>
<HEAD>
<TITLE>Company Feedback</TITLE>
</HEAD>
<BODY>
Are you sure you want to delete the record

<?php print "<a href=\"feedback_delete.php?final_del=$_GET[id]\"> YES </a>"; ?>
&nbsp;&nbsp;&nbsp;
<a href="feedback_pg.php"> NO </a>

</BODY>
</HTML>

 

BY THE WAY-Delete with caution: When I was learning programming and I do a DELETE, the deleting goes straight and wipe out the record, later my client now ask that they sometimes delete an important record by mistake, this makes me to device this new algorithm for handling this task by first warning the user to confirm their delete. 
And that is what I use in the DELETE code above.

BY THE WAY-Reducing number of pages: this delete program unlike the former ones, where I have to collect a data in another page let’s say contact.php and submit / process in another let’s say contact_process.php both operation is done in one page, but you will have to use if(isset(var)){ code block } to check within the page if the variable (var) is set or hold a data, to determine if the code block within the if{ } statement will be process. This is important because PHP process your code sequentially and you don’t want this code block be process until you click a submit button or link to activate this operation. This is the approach I use for the DELETE program above. Let’s explain the program.

 

I bolded the block of code (which come first) and a line of code (which come second) in the DELETE program which need some explanation, unlike the other program the second (line of code) actually come first in execution and it call the first (block of code) to take action.

 

 

BELOW IS AN ILLUSTRATED SCREENSHOT OF THE FLOW OF THIS OPERATION

 

HERE IS THE EXPLANATION of the flow

A.            From the list of record in feedback_pg.php, I click the Delete link in front of the ddd… record, as shown in the screenshot below.


B.            this will lead me to the next page feedback_delete.php where I get the warning message


 If I click NO I will be returned to back to the previous page (feedback_pg.php) and if YES the DELETE query will be executed (gotten from the program in the feedback_delete.php page, here is the DELETE program segment)

if(isset($_GET['final_del'])){
     //get the id from the confirmation link
     $query_id = mysql_real_escape_string($_GET['final_del']);

     //the deleting query
     $query  = "DELETE FROM feedback WHERE id = '$query_id'";
     $result = mysql_query($query) or die(mysql_error());
    
     //redirect back after deleting
     header("Location:feedback_pg.php");
}

As said earlier, the querystring attached to the YES link makes the code block with the... if(isset($_GET['final_del'])){ to be executed, using the id $query_id to delete that particular record... DELETE FROM feedback WHERE id = '$query_id' after the deletion then you will be redirected to the previous page feedback_pg.php... header("Location:feedback_pg.php"); with the record deleted.

 


Return to PHP Sample Main Page