Delete Employee Page

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:

  1. a form and form elements
  2. a recordset

The reasons these elements are not needed is because:

  1. the page is used only to execute a PHP script
  2. 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

  1. 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.

  2. 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.

    <?php
    require("db.php");
    $pdo_statement=$pdo_conn->prepare("DELETE from employees WHERE ID=" . $_GET[recordID]);
    $pdo_statement->execute();
    ?>

  3. 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.

  1. 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.

  2. 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.