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

Posted on 2007-09-29
Last Modified: 2008-09-05
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?
Question by:jeveloff2
    LVL 28

    Accepted Solution

    <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.
    LVL 11

    Assisted Solution

    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');

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now