PHP & MySQL - How to Show Selected value on Dropdown Menu

PHP & MySQL - How to Show Selected value on Dropdown Menu

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP & MySQL - How to Show Selected value on Dropdown Menu

You're seeing all data in your form correctly, but the dropdown list is showing the default first option and the textarea remains blank. This happens because of two specific mistakes in how you are handling variables and attributes in HTML. Let's break down exactly what's going wrong and how to fix it.

The first issue lies in the <select> element itself. In your code, you have value="<?php echo $rows->agama;" directly on the opening <select> tag. The value attribute on a <select> tag has absolutely no effect. It does not define which option is currently selected; it defines the value of the form field when submitted, but that is already controlled by the nested <option> tags. To visually indicate a selection, you must add the selected attribute to the specific <option> tag that matches your data from the database.

Furthermore, there is a critical bug in how you are fetching and accessing the data. You are using mysql_fetch_array($qryTampil), which returns an indexed array or an associative array depending on the second parameter. However, in your PHP code, you reference $rows. This variable does not exist in your scope; it should be $dataTampil. Additionally, you are using the deprecated mysql_query and mysql_fetch_array functions. These were removed in PHP 7.0 because they are insecure and lack error handling. If you are maintaining legacy code, ensure you update to mysqli or PDO immediately.

Here is how you should structure the dropdown logic correctly:

<select name="agama" id="agama">
    <option value="Islam">Islam</option>
    <option value="Khatolik" <?php echo ($dataTampil['agama'] == 'Khatolik') ? 'selected' : ''; ?>>Khatolik</option>
    <option value="Protestan" <?php echo ($dataTampil['agama'] == 'Protestan') ? 'selected' : ''; ?>>Protestan</option>
    <option value="Hindu">Hindu</option>
    <option value="Buddha">Buddha</option>
    <option value="Lain-Lain">Lain-Lain</option>
</select>

Notice the pattern here. We check if the database value matches the option's value. If they match, we inject the selected attribute. This is safer than trying to pass a single string to the <select> tag.

The second issue involves the textarea. You have placed a value attribute on your <textarea>:

<textarea name="alamatkorban" id="alamatkorban" value="<?php echo $dataTampil['alamatkorban']; ?>">
</textarea>

This is invalid HTML. The <textarea> tag does not support a value attribute. Unlike input elements, textareas are self-closing in terms of content; the content between the opening and closing tags represents the initial value. To display data retrieved from MySQL, you simply echo that data inside the tags:

<textarea name="alamatkorban" id="alamatkorban"><?php echo htmlspecialchars($dataTampil['alamatkorban']); ?></textarea>

Using htmlspecialchars is essential here to prevent Cross-Site Scripting (XSS) attacks. If your database contains malicious JavaScript, echoing it directly into a textarea (and subsequently if that content were ever rendered in an innerHTML context later) could compromise user security.

Regarding your SQL query, $sqlTampil = "select * from data_korban Where kasus_id=$id";, you are vulnerable to SQL injection because the $id variable is concatenated directly into the string. Always use prepared statements with mysqli or PDO to sanitize inputs like $id.

If you are starting a new project or looking to refactor this legacy application, consider migrating to Laravel. Frameworks like Laravel provide robust built-in helpers for fetching data and rendering forms that automatically handle these selection states and security concerns. For example, Laravel's Blade engine allows you to use the old() helper for validation errors and makes populating dropdowns much cleaner with conditional logic. You can read more about building secure applications with modern tools at https://laravelcompany.com.

By correcting the attribute usage on the select element and placing the data correctly inside the textarea tags, your form will display the existing record accurately upon editing.

Our network