Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Load the value saved for the record being edited, then compare it with each dropdown option and add selected to the matching option. For a customer relationship, use the customer’s unique ID as the option value when possible, rather than the display name.

Why the dropdown does not select the saved value

The selected value must come from the record being edited. It is separate from the rows used to populate the dropdown. In the SitePoint Forums example, the code compared an entire fetched $row array with an option value, and it did not assign a value to $selectedValue. A scalar option value cannot match the full row array, and an unset selected value gives the comparison nothing useful to check.

The original discussion is from March 29, 2019, and is useful for understanding that mistake, not as current official PHP documentation: SitePoint Forums: Get current value from database and and show as selected on edit form page.

Use the saved customer ID to select an option

First load the customer ID associated with the record being edited and make it available before rendering the form. Also load the customers for the option list. In this illustration, $currentCustomerId is the saved ID, and $customers contains customer rows with id and customer_name fields; adapt those names and the loading code to your schema.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<select name="customer_id">
  <?php foreach ($customers as $customer): ?>
    <option value="<?= htmlspecialchars((string) $customer['id'], ENT_QUOTES, 'UTF-8') ?>"
      <?= (string) $customer['id'] === (string) $currentCustomerId ? ' selected' : '' ?>>
      <?= htmlspecialchars($customer['customer_name'], ENT_QUOTES, 'UTF-8') ?>
    </option>
  <?php endforeach; ?>
</select>

For each customer, the code compares the option’s ID with the saved ID. When they match, it emits the HTML selected attribute, so the browser displays that customer when the form opens. The values are converted to strings so a database integer and a submitted or otherwise loaded string can be compared consistently.

Use a name only if it is a suitable identifier

If the table has no customer ID and names are unique for the purpose of this form, you can use customer_name as both the option value and the saved value. Compare those scalar strings in the loop and emit selected on a match. Names are often editable or duplicated, however, so an ID is generally a more reliable value for a form that updates a relationship.

Keep the edit value and option list distinct

  • Saved value: the customer ID or name currently stored on the record being edited.
  • Option list: the available customers, each with its own ID and display label.
  • Comparison: the saved scalar value compared with the current option’s corresponding scalar value.

Escaping the option value and label with htmlspecialchars helps prevent database content from being interpreted as HTML. When the form is submitted, validate that the submitted customer ID is allowed for the current operation; rendering a selected option does not perform that validation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When AJAX is unnecessary

If the page already has both the edited record’s saved value and the customer list before it renders the form, server-rendered options can include the selected state directly. The SitePoint thread’s later AJAX example is questioned in the discussion because it posts to the same page and its response appears unused; the excerpt does not establish a need for AJAX or verify that code as a working solution.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.