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

  • SEARCH
  • Home
  • 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 6216157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:11:06+00:00 2026-05-24T07:11:06+00:00

I have a database with the following table: CATEGORY id name Another entity in

  • 0

I have a database with the following table:

CATEGORY
id
name

Another entity in the database can be assigned any of these categories through a foreign key on that entity. In my PHP application, I want a dropdown option select box to show the current category by default, but also allow any of the others to be selected in order to change the category that the entity belongs to.

In trying to formulate a SQL query to populate the dropdown select options that shows all the categories starting with the currently selected one, I was trying different things and came up with this:

SELECT name FROM category ORDER BY id=[ID] DESC

Which seems to work properly given my small set of sample data, but seeing as I was just guessing around, I’m not 100% confident this is the proper query and will continue to work with the full dataset. I’m hoping someone can confirm that this SQL is appropriate, or suggest if there is a better way to accomplish this.

Also, the form action links to a page that contains the UPDATE query, and currently I am getting the category ID back by first running a SELECT query for the category name, but is there a way I can just assign the category ID for each category to it’s respective select option?

Thanks!

  • 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-24T07:11:06+00:00Added an answer on May 24, 2026 at 7:11 am

    I’m not sure your best option is re-ordering the select instead of simply making the particular option selected (example below).

    Your query should work consistently because id=[id] is a boolean which will then be converted to a 0 or a 1 depending on whether it is true or false. I do wonder about the index in such cases — MySQL will not be likely to compensate for that. You may wish to swap that with a UNION DISTINCT, but you’ll have to benchmark yourself:

    -- possible UNION DISTINCT
    SELECT * FROM CATEGORY WHERE ID = [id]
    UNION DISTINCT
    SELECT * FROM CATEGORY;
    

    As to getting the ID from the <select> directly, you can always use the value property of the options:

    <select name="mySelect">
        <option value="1">Green</option>
        <option value="2">Blue</option>
    </select>
    

    In the above, if green is selected, PHP will receive $_REQUEST['mySelecte'] == '1'.

    FINAL PHP EXAMPLE

    $selectedID = $_REQUEST['mySelect'];
    // if you absolutely *have* to make the selected item first, replace the 
    // following with one of the two queries above
    $q = mysql_query( 'SELECT id, name FROM CATEGORIES' ) or die( mysql_error() );
    echo '<select name="mySelect">';
    while( $option = mysql_fetch_assoc( $q ) )
    {
        echo '<option value="' . $selectedID . '"';
        if( $option[ 'id' ] == $selectedID ) echo ' selected="selected"';
        echo '>' . $option['name'] . '</option>' . PHP_EOL;
    }
    echo '</select>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with the following structure - Category {Id, Name, ParentId} I
I have a database with the following tables: create table Categories ( Id int
I have a database with the following table: PATIENT (PATIENT_ID*, MEDICAL_EXAMINATIONS) where the field
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
Let's say I have the following database table: record_id | record_date | record_value -----------+-------------+--------------
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following table in a SQLite3 database: CREATE TABLE overlap_results ( neighbors_of_annotation
Say I have a database table like the following: FileID | FileName | FileSize
I have no control over database schema and have the following (simplified) table structure:

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.