Hello everyone,
I have a problem with my drop down list box on my edit page in php. I would like my drop down list box to show what the user has selected, then if the user wishes to, change their selection updating the DB(mysql) at the same time. I have a couple of radio buttons that does the same thing and works fine.

eg <?php if ($dataArow['garageopt'] == "yes"){ echo ' checked="checked"';}?> />

When i try to apply the same method changing the checked=checked to selected it doesen't work.

<select name="cartype" />   
<option value="<?php if ($dataArow['cartype'] == "tvr"){ echo ' selected="selected"';}?>" />tvr</option>
<option value="<?php if ($$dataArow['cartype'] == "lexus"){ echo 'selected';}?>"/>lexue</option>
<option value="<?php if ($$dataArow['cartype'] == "porche"){ echo 'selected';}?>"/></option>
</select>

Can somebody help me please it would be much appreciated.

Thanks.

Dani AI

Generated

Short summary and practical follow-ups based on the thread between and .

The root cause in this thread was not the HTML select itself but the data used to populate it: the array used to set the selected option was empty because the SELECT that should supply $dataArow returned no rows (so the browser fell back to the first option). The original snippets also showed a few common pitfalls: injecting PHP into the value attribute instead of setting selected on the option, accidental $$ variable typos, and invalid self-closing select/option markup. Those mistakes make the HTML or logic do the wrong thing even if the database is fine.

Quick checklist to reproduce and fix the problem

  • Confirm the SELECT query actually returns the record (use var_dump/print_r or check rowCount/mysql_num_rows).
  • Verify the same id/key is used for the UPDATE and the later SELECT; a mismatched id will make the form show stale data.
  • After running the UPDATE, either re-query the row or use a redirect (POST-Redirect-GET) so the page shows the updated values.
  • Ensure option value attributes are plain data and the selected attribute is applied to the option element (not injected into the value).
  • Watch for PHP syntax bugs (extra $, misspelled field names) and for output sent before a header() redirect.

Example flow (safe, requery-after-update / PRG pattern)

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // validate and update with prepared statement
    $stmt = $pdo->prepare('UPDATE cars SET cartype = :type WHERE id = :id');
    $stmt->execute([':type' => $_POST['cartype'], ':id' => $_POST['id']]);
    header('Location: edit.php?id=' . urlencode($_POST['id']));
    exit;
}
// on GET: fetch the fresh row to populate the form
$stmt = $pdo->prepare('SELECT id, cartype FROM cars WHERE id = :id');
$stmt->execute([':id' => $_GET['id']]);
$dataArow = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($dataArow); // quick debug to confirm data

Final notes: prefer PDO or mysqli with prepared statements (avoid deprecated mysql_*), use PRG to avoid stale state or duplicate submits, and re-query after update so the select shows the current value. The thread shows the correct diagnosis — an empty data row — and the fixes above make the selected option stable after an update.

Recommended Answers

All 9 Replies

try:

<select name="cartype" />   
<option value="tvr" <?php if ($dataArow['cartype'] == "tvr"){ echo ' selected="selected"';}?> />tvr</option>
<option value="lexus" <?php if ($dataArow['cartype'] == "lexus"){ echo 'selected="selected"';}?> />lexus</option>
<option value="porche" <?php if ($dataArow['cartype'] == "porche"){ echo 'selected="selected"';}?> /></option>
</select>

try:

<select name="cartype" />   
<option value="tvr" <?php if ($dataArow['cartype'] == "tvr"){ echo ' selected="selected"';}?> />tvr</option>
<option value="lexus" <?php if ($dataArow['cartype'] == "lexus"){ echo 'selected="selected"';}?> />lexus</option>
<option value="porche" <?php if ($dataArow['cartype'] == "porche"){ echo 'selected="selected"';}?> /></option>
</select>

Thanks for your help, but it doesn't seem to work quit as much as i would like it to. The DB updates thats great, but when the edit page reloads the drop down list box goes back to the first option (tvr),and not to the one that was selected for update eg porche.

Well, let's run a little test. After the select, add this bit of code:

<!-- <?php echo $dataArow['cartype']; ?> -->

That way, we'll see just what $dataArow contains. If it is returning empty, then nothing in the code would be set to "selected", therefore, the first option would always be selected.

Well, let's run a little test. After the select, add this bit of code:

<!-- <?php echo $dataArow['cartype']; ?> -->

That way, we'll see just what $dataArow contains. If it is returning empty, then nothing in the code would be set to "selected", therefore, the first option would always be selected.

Hi Wraithmanilian,
The code you provided show no change, on reload, the first option is returned (tvr).

Hi Wraithmanilian,
The code you provided show no change, on reload, the first option is returned (tvr).

With that page rendered, right-click in your browser and view the source. Look under the select and see what is posted between the <!-- and -->

With that page rendered, right-click in your browser and view the source. Look under the select and see what is posted between the <!-- and -->

Nothing.

Nothing.

Then THAT means that the variable $dataArow is empty, and since it has to equal SOMETHING in order to choose what sets the selected="selected", then nothing gets chosen, therefore, nothing changes.

The problem is that you are returning zero records. It's a MySQL issue.

Then THAT means that the variable $dataArow is empty, and since it has to equal SOMETHING in order to choose what sets the selected="selected", then nothing gets chosen, therefore, nothing changes.

The problem is that you are returning zero records. It's a MySQL issue.

Got it workig now. Thanks for your advice.

Got it workig now. Thanks for your advice.

You're welcome. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.