Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6708643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:47:04+00:00 2026-05-26T07:47:04+00:00

I have a dropdown menu populated from a table in a MySQL database. It

  • 0

I have a dropdown menu populated from a table in a MySQL database. It functions, but I would like to add an if/else statement to the code to change the default value of the dropdown according to the conditions.
Here is the code for the dropdown (much thanks to AlienWebguy for his assistance in getting this far):

    <?php
    $sql="SELECT techID, tech_userlogin FROM technicians";
    $result=mysql_query($sql);

    while ($row=mysql_fetch_array($result)) 
    {
        $techID = $row['techID'];
        $tech_userlogin=$row['tech_userlogin'];

        // Here - check if the $_GET value in the query matches the tech_userlogin
        ($selected = ($tech_userlogin == $_GET['wtech_userlogin']) ? 'selected="selected"' : '');
        echo '<option value="' . $tech_userlogin . '" ' . $selected . '>' . $tech_userlogin . '</option>' . "\n";
    }
?>

I’ve tried a couple of things to accomplish what I’m after, but I’m severely hampered by my lack of experience and/or expertise in PHP/MySQL.

This is the first time I’ve tried these things and it has been hit and miss so far. I’ve recieved help here at Stack Overflow and I’m grateful such a resource exists.

This is my latest effort and it fails to achieve what I’m after. I want the default value of the dropdown to be “–Select Technician–” if the condition is not met in the if statement.

<?php
    $sql="SELECT techID, tech_userlogin FROM technicians";
    $result=mysql_query($sql);

    $options="";

    while ($row=mysql_fetch_array($result)) 
    {
        $techID = $row['techID'];
        $tech=$row['tech_userlogin'];
        $options.="<option value=\"$tech\">$tech</option>";

        // Here - check if the $_GET value in the query matches the tech_userlogin
        if
        ($selected = ($tech == $_GET['wtech_userlogin']) ? 'selected="selected"' : '')
        {echo '<option value="' . $tech . '" ' . $selected . '>' . $tech . '</option>' . "\n";}

        else
        {echo '<option value="' . $options . '" .  'selected="selected"'>'--Select Tech--'<. $options .>' . "\n";}
    }
?>

Can someone please help me or point me in the right direction? Many thanks.

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:47:05+00:00Added an answer on May 26, 2026 at 7:47 am

    Try this

    <select name="whatever">
        <option value="">-- Select Technician --</option>
        <?php while ($row = mysql_fetch_array($result)) :
            $techId = $row['techID']; // why isn't this used?
            $tech = htmlspecialchars($row['tech_userlogin']);
            $selected = $row['tech_userlogin'] == $_GET['wtech_userlogin'] ? '" selected="selected' : '';
        ?>
        <option value="<?php echo $tech, $selected ?>"><?php echo $tech ?></option>
        <?php endwhile ?>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dropdown menu that is filled by a mysql database. I need
Basically, I have populated a dropdown list using php from a database on a
I have a dynamically populated PHP dropdown menu that gathers the following information from
I have a little dropdown menu similar <select> , which contolled from external js-function.
I have created a dropdown menu with jquery: here Everything is ok but in
I want to insert a value from the database into my dropdown menu in
I have a dropdown menu that is populated with key value pairs in php.
I have a dropdown menu bounded with values from the db on form load.
I have this dropdown (select) menu generated with JQuery, dynamically using JSON coming from
I have a dropdown menu. Its height is animated with jQuery from 5px to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.