Select option menu read from database and use it's values

Select option menu read from database and use it's values

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your PHP Dropdown Only Returns the First Option

You've built a standard dropdown menu using mysql_* functions and a simple SELECT query to fetch categories from your tnews2 database. The HTML structure looks correct, and the loop iterates through the results to populate <option> tags. However, when you submit the form, $_POST['categories'] only contains the value of the first item in the list, or worse, it seems to ignore the selection entirely.

This behavior usually stems from a mismatch between how the HTML form submits data and how your PHP script interprets the name attribute of the <select> element versus the value attribute of the individual options.

The Core Issue: Missing Name Attributes

In your provided code snippet, the <select> tag has a name="categories" attribute. This is correct; it tells the browser that this entire control group should be sent to the server with the key categories. However, inside your PHP loop, you are generating the options like this:

<option value=" <?php $row['path']; ?> ">
    <?php echo $row['name']; ?>
</option>

Notice that the <option> tag itself does not have a name attribute. This is technically optional for standard HTML5, but it can cause confusion in older browsers or specific form-handling scripts if multiple selects exist on the same page. More importantly, ensure that your database column actually contains the data you expect.

Your query selects id and name, but your output loop tries to echo $row['path']. If your table structure is named categories and columns are id, name, and path, this works fine. However, if the column name in the database is actually different (e.g., just id or slug), you will be sending empty strings or incorrect values to the form. The form submits whatever is inside the value="..." attribute. If that value is empty because of a mismatched array key, the form submission will register as an empty string for every selection except potentially the first one depending on browser quirks with empty inputs.

Handling Multiple Selections vs. Single Selection

If your intention is to allow users to select multiple categories, the HTML structure needs to change significantly. A standard <select> tag with a single name attribute will only return one value, even if the user selects multiple items in a "multiple" checkbox-style dropdown.

To support multiple selections, you must append an index to each option's name:

<select name="categories[]" multiple="multiple">
    <option value="cat-1">Technology</option>
    <option value="cat-2">News</option>
</select>

In this scenario, $_POST['categories'] would return an array. If you want to stick to a single selection (which is standard for categories), ensure no other <select> or <input type="hidden"> elements with the name categories are present on the page. Duplicate names in a form cause only the last one submitted to be processed by the server.

Modernizing Your Stack

The code snippet you provided relies on the mysql_* functions, which were deprecated in PHP 5.5 and removed entirely in PHP 7.0. Continuing to use these functions poses a significant security risk due to SQL injection vulnerabilities and prevents you from leveraging modern features like prepared statements.

To fix this robustly, migrate to PDO or the MySQLi extension immediately. Here is how you would refactor your database connection and query using PDO with prepared statements:

try {
    $pdo = new PDO("mysql:host=localhost;dbname=tnews2", "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Prepare the statement to prevent SQL injection
    $stmt = $pdo->prepare("SELECT name, id FROM categories ORDER BY ID DESC LIMIT 0,6");
    $stmt->execute();
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo '<option value="' . htmlspecialchars($row['id']) . '">' . htmlspecialchars($row['name']) . '</option>';
    }
} catch (PDOException $e) {
    die("Database error: " . $e->getMessage());
}

When processing the POST request, always verify that $_POST['categories'] actually contains a value before using it in your logic. Check if it is not empty (!empty($_POST['categories'])) and ensure it isn't just whitespace. If you are handling multiple values now, iterate through the array to assign tags or filter content accordingly.

For further guidance on securing your application and modernizing legacy codebases with Laravel, resources like https://laravelcompany.com can provide comprehensive documentation on best practices for database interaction and form handling in modern PHP ecosystems.

Our network