> For the complete documentation index, see [llms.txt](https://reyvaleza.gitbook.io/vibe.d-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://reyvaleza.gitbook.io/vibe.d-tutorial/retrieving-a-record-for-editing.md).

# Retrieving a record for editing

First, we need to retrieve the record to be edited. There is an **id** field in the **employees** table that has a unique number for each row, and this field is used to identify the row (being the primary key).

We also included a hidden field in the **views\emplistall.dt** file: the **id** field, which has the value of the **id** field in the **employees** table, and which we can use as a key to retrieve the record so we can populate the form with the employee data:

&#x20;             `input(type="hidden", name="id", value="#{e.id}")`

We can use that hidden field then.&#x20;

Edit **source\empcontrol.d** and append this method:

```
  void getEditEmployee(int id)
  {
    Employee e = empModel.getEmployee(id);
    render!("empedit.dt", e, departments, paygrades, provinces);
  }
```

This method calls the **empModel.getEmployee()** method with the **id** as the argument, then receives the returned Employee with the needed data, which is used to populate the form **empedit.dt**, which we should create next.

Edit **source\empmodel.d** and append this method:

```d
  Employee getEmployee(int id)
  {
    string sql = "select * from employees where id=?";
    Prepared pstmt = conn.prepare(sql);
    pstmt.setArgs(id);
    Employee e;
    Row[] rows = conn.query(pstmt).array;
    if(rows.length == 0) return e;
    return prepareEmployee(rows[0]);
  }
```

Then create **views\empedit.dt**.

```
extends layout
block maincontent
  include cssformgrid.dt
  div.form-grid-wrapper
    h2.center-align Edit employee details
    form.form-grid(method="post",action="edit_employee",enctype="multipart/form-data")
      label.form-grid-label Department
      select#deprt.form-grid-input(name="e_deprt", value="#{e.deprt}")
        -foreach(dep; departments)
          -if(dep == e.deprt)
            option(value="#{dep}", selected) #{dep}
          -else
            option(value="#{dep}") #{dep}
      label.form-grid-label Salary grade
      select#paygd.form-grid-input(name="e_paygd", value="#{e.paygd}")
        -foreach(pay; paygrades)
          -if(pay == e.paygd)
            option(value="#{pay}", selected) #{pay}
          -else
            option(value="#{pay}") #{pay}
      label.form-grid-label Email address
      input.form-grid-input(type="email", name="e_email", value="#{e.email}")
      label.form-grid-label Password
      input.form-grid-input(type="password", name="e_pword", value="#{e.pword}")
      label.form-grid-label First name
      input.form-grid-input(type="text", name="e_fname", value="#{e.fname}")
      label.form-grid-label Last name
      input.form-grid-input(type="text", name="e_lname", value="#{e.lname}")
      label.form-grid-label Phone
      input.form-grid-input(type="text", name="e_phone", value="#{e.phone}")
      label.form-grid-label Street address (no city)
      input.form-grid-input(type="text", name="e_street", value="#{e.street}")
      label.form-grid-label City
      input.form-grid-input(type="text", name="e_city", value="#{e.city}")
      label.form-grid-label Province
      select#province.form-grid-input(name="e_province", value="#{e.province}")
        -foreach(prov; provinces)
          -if(prov[0] == e.province)
            option(value="#{prov[0]}", selected) #{prov[1]}
          -else
            option(value="#{prov[0]}") #{prov[1]}
      label.form-grid-label Postal code
      input.form-grid-input(type="text", name="e_postcode", value="#{e.postcode}")
      label.form-grid-label ID Picture
      input.form-grid-input(type="file", name="picture")
      input(type="hidden", name="e_photo", value="#{e.photo}")
      input(type="hidden", name="e_id", value="#{e.id}")
      div
      div
        a(href="all_employees")
          button.form-grid-button(type="button") Cancel
        input.form-grid-button(type="submit", value="Submit")
```

The form is displayed using the **Employee** data that was passed to it.

Compile, run and refresh the browser and click on a pencil icon to edit a record.

Great, a record was opened for editing.

<figure><img src="/files/chFfSR1CxWANXPHJfCYy" alt=""><figcaption></figcaption></figure>

But after clicking Submit, we get this error:

<figure><img src="/files/SK066WlCZRELdO0gG5RR" alt=""><figcaption></figcaption></figure>

Because we haven’t defined the method to save the changes to the database. Let’s do that next.<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://reyvaleza.gitbook.io/vibe.d-tutorial/retrieving-a-record-for-editing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
