SOLVED: Customer Dropdown Showing Two Selected Options on Edit
Problem: When editing a record, the customer dropdown (`debtor_no`) was incorrectly showing two selected customers. The correct customer ID for the record was present in the `$_POST` data, but the dropdown also selected the record's primary key ID.
Cause: The issue stemmed from how the `$_POST` array was populated during the edit operation. Instead of explicitly setting each form field value from the fetched database row (`$myrow`) to its corresponding associative key in `$_POST`, the entire `$myrow` array was directly assigned to `$_POST`. This resulted in `$_POST` containing both associative keys (column names) and numerical indexes (based on the order of columns fetched).
The `combo_input` function, used to generate the dropdown, was likely accessing the `$_POST` array and inadvertently picking up the value at index `0` (which was the record's primary key `id`) in addition to the correct `debtor_no`. This led to both IDs being included in the `$selected_id` array processed by `combo_input`, causing both options to be marked as `selected` in the HTML.
Fix: To resolve this, ensure that you explicitly set each relevant form field value in the `$_POST` array using the associative keys from your fetched database row.
Incorrect (Causing the Issue):
ie. Don't be lazy like me
Correct (Explicitly Setting Values):
$_POST['debtor_no'] = $myrow['debtor_no'];
$_POST['other_field'] = $myrow['other_field'];
// ... and so on for all your form fields
By explicitly assigning the values, you prevent the numerical indexes from being present in the `$_POST` array and thus avoid the `combo_input` function mistakenly selecting the record's primary key ID.