Deleting a record from the database

When a user clicks on the trash bin icon, it should not immediately delete the corresponding record. The user should be given the chance to recover if the user made a mistake. We can simply display a page showing the employee details first, then ask the user for confirmation.

Edit source\empcontrol.d and add this code:

  void getDeleteEmployee(int id)
  {
    Employee e = empModel.getEmployee(id);
    render!("empdelete.dt", e);
  }

We retrieve the employee record and display that record in views\empdelete.dt.

So let's create views\empdelete.dt.

extends layout
block maincontent
  include cssformgrid.dt
  div.form-grid-wrapper
    h2.center-align Are you sure you want to delete this record?
    form.form-grid(method="post", action="delete_employee")
      span.form-grid-label Employee number:
      span.form-grid-field #{e.depid}
      span.form-grid-label Department:
      span.form-grid-field #{e.deprt}
      span.form-grid-label Salary grade:
      span.form-grid-field #{e.paygd}
      span.form-grid-label Email address:
      span.form-grid-field #{e.email}
      span.form-grid-label First name:
      span.form-grid-field #{e.fname}
      span.form-grid-label Last name:
      span.form-grid-field #{e.lname}
      span.form-grid-label Phone:
      span.form-grid-field #{e.phone}
      span.form-grid-label Street address:
      span.form-grid-field #{e.street}
      span.form-grid-label City:
      span.form-grid-field #{e.city}
      span.form-grid-label Province:
      span.form-grid-field #{e.province}
      span.form-grid-label Postal code:
      span.form-grid-field #{e.postcode}
      span.form-grid-label ID Picture:
      img(src="#{e.photo}", height="80px")
      input(type="hidden", name="id", value="#{e.id}")
      div
      div
        a(href="all_employees")
          button.form-grid-button(type="button") Cancel
        input.form-grid-button(type="submit", value="Delete")

Here we are displaying a page showing the employee details and asking the user for confirmation.

Compile, run and refresh the browser. Show the list of employees again and click on a trash bin icon. The confirmation page should show.

Click Cancel for now just to test if the Cancel button works. Then click the trash bin icon of another record.

This time click the Delete button.

And you get this error:

So let’s implement postDeleteEmployee() in EmployeeController class.

Edit source\empcontrol.d and append this code.

  void postDeleteEmployee(int id)
  {
    empModel.deleteEmployee(id);
    redirect("all_employees");
  }

We are calling empModel.deleteEmployee() here, so let’s implement that too.

Edit source\empmodel.d and append this code.

  void deleteEmployee(int id)
  {
    string sql = "delete from employees where id=?";
    Prepared pstmt = conn.prepare(sql);
    pstmt.setArgs(id);
    conn.exec(pstmt);
  }

Here we finally delete the record from the database table.

Compile, run and refresh the browser to the list of employees. Click on a trash bin icon, click Delete on the next screen, and you will be redirected to the list of employees again.

This time you should see that the record you selected is no longer listed.

Now, let’s implement that Find employee link on the menu.

Last updated