Vibe.d tutorial
  • Build web apps with Vibe.d
  • Setting up
  • The default hello world app
  • Use your own HTML page
  • Serving other static files
  • Diet templates
  • More template pages
  • Use your own functions
  • Templates for ease of maintenance
  • Using include in templates
  • Responsive layout using CSS Grid
  • A fixed navbar and sticky footer
  • CSS modal dialogues
  • The web interface
  • Setting up MySQL server and tools
  • The schema
  • Setting up MySQL for Vibe.d
  • The EmployeeController class
  • The EmployeeModel class
  • A form for adding a new employee
  • Saving form data into the database
  • Testing the whole thing
  • Listing all the employees
  • Retrieving a record for editing
  • Saving form changes to the database
  • Deleting a record from the database
  • Finding an employee record by name
  • Displaying error messages with _error
  • Authentication and authorization
  • Logging in and authentication
  • Saving the login state to the session
  • Enforcing authorization through the session
  • Logging out
  • All the sources so far
  • A new project
    • The timekeeping system
Powered by GitBook
On this page

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.

PreviousSaving form changes to the databaseNextFinding an employee record by name

Last updated 5 months ago