Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

Automatically selecting a value in a drop down field from the MYSQL value

I'm pretty new to PHP/MYSQL.  I have a customer database which has numerous drop down fields when adding a new customer.  I made these drop down fields as normal HTML drop downs instead of arrays.  The problem I am having is when we edit a customer.  The customer fields used to be text boxes and I just had value="<?php  echo $name; ?>" in each field for editing.  I do not know how to have the script automatically select the value from the database in the drop down field.  Any ideas?
2 Solutions
<select name="whatever">
    <option value="<?php echo $value; ">$value ?></option>
    <option value="abc">ABC</option>
    <option value="def">DEF</option>

Replace $value by the appropriate variable name.
If you wanted a dropdown populated with items from a db field, and you wanted the item "cheese" selected you could do something like this:

    echo('<select name="select_name">\r\n');
    $recordSet = $db->Execute("SELECT field FROM products");
    foreach ($recordSet as $row) {
        $selected = ($row = 'cheese' ? ' selected = "selected"' : '');
        echo('<option value=' & $row & $selected & '>' & $row & '</option>\r\n');

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now