Like the add operation, the delete operation is another one of the easiest of the other three operations (e.g., edit, update, and select) to implement. Yet, it is the most dangerous process with the capability of deleting ALL of the records in the database if not done right. If you are new to database development, it is best to backup the database if you can because the delete process is irreversible.
Unlike the other pages, only PHP code is needed for the DELETE EMPLOYEE page. There is no need to create:
a form and form elements
a recordset
The reasons these elements are not needed is because:
the page is used only to execute a PHP script
a user will never see this page
The code that will be created will perform the following tasks:
ADD DYNAMIC SCRIPTS
Add SQL DELETE Script
Add Redirect Script
ADD DYNAMIC SCRIPTS
Add SQL DELETE Script
Open the delete_employee.php page that you created earlier and DELETE ALL the existing elements in the HTML file. WHY: This will be a PURELY PHP script page with no HTML tags in it.
TIP: Press CTRL+A and then click the DELETE button.
Add the following highlighted code. WHY: To create a script (not a recordset) to delete a record from the database based on the recordID variable passed from the EMPLOYEE DETAIL page when the DELETE EMPLOYEE button is pressed.
CAUTION: While the WHERE clause is optional, If you inadvertently forget to include it, you will delete ALL of the records from the database.
<?php require("db.php"); $pdo_statement=$pdo_conn->prepare("DELETE from employees WHERE ID=" . $_GET[recordID]); $pdo_statement->execute(); ?>
CODE EXPLANATION:
It is a common mistake to write DELETE * FROM myTable. However, unlike the SELECT clause, it is important to note that you DO NOT select COLUMNS from the database because you are deleting a complete ROW that cuts across all of the columns for that record.
Because there is no need to DISPLAY anything on the Delete page, there is no need to store a reference of the query like we did on the ADD EMPLOYEE page, etc.
Save the file.
Add Redirect Script
After a page is submitted and data added to the database, a user needs to be REDIRECTED to the EMPLOYEE MASTER PAGE (master_page.php). You could also create a separate confirmation page if you wanted or needed to.
Write the following highlighted code within the PHP block code. WHY: To redirect the user to the EMPLOYEE MASTER PAGE.
<?php require("db.php"); $pdo_statement=$pdo_conn->prepare("DELETE from employees WHERE ID=" . $_GET[recordID]); $pdo_statement->execute(); header('location:master_page.php'); ?>
CODE EXPLANATION: - If PHP detects an error, the redirect code will not be executed.
CHECKPOINT: Save the file and then open the EMPLOYEE MASTER PAGE (master_page.php) and click a employee list item that you would like to delete. Then, on the EMPLOYEE DETAIL page, click the DELETE EMPLOYEE button. You should be prompted to ensure that you want to delete that employee. Click the OK button. You should see that the page immediately returned to the EMPLOYEE MASTER PAGE and the employee that was deleted is no longer in the ListView.