Skip to content
Advertisement

with two different options not from database, but custom text php mysql, html

I have my database in phpmyadmin, and I have a table ’employees’. One of the fields is ‘pay_item’, and this particular field is type ‘varchar(100)’.

I want to have a dropdown in my php website, and I want it to have two different options: ‘Hourly’ and ‘Salary’.

I have got it to retrieve the values from the DB, so whatever that employee has selected, it will come up no the screen, and that ‘pay_item’ will be selected on the dropdown.

The problem I have right now, is that when I submit my form, it is not sending the selected choice to the DB. Say I want to edit from one to the other, it won’t take the change.

I know how to write using a ‘while’ loop, and populate from the DB, but in this case, I just want to have these two options, and not use a while, since there is not a specific table for ‘pay_items’

This is the code I have so far:

        <td colspan="3">
        <select name="pay_item">

            <?
                echo"   <option value="$pay_item"";
            if($pay_item=="Regular Hourly")
                echo" selected";
                echo">Regular Hourly</option>n";
            
                echo"   <option value="$pay_item"";
            if($pay_item=="Regular Salary")
                echo" selected";
                echo">Regular Salary</option>n";

            ?>
            </select>
        </td>

Now, I forgot to mention that when it is on ‘Regular Salary’, it DOES take the change to ‘Regular Hourly’, but not the other way around, so I assumed there must be something wrong with the structure of the .

Before, I had this as a textfield, so people were entering ‘Regular Hourly’ and so, but from time to time, they would enter grammatical errors, so I am trying to avoid that with a dropdown.

The way I am sending this to the DB, is with a normal UPDATE or INSERT query.

$db_link->query("INSERT INTO employees (pay_item) VALUES ('$pay_item')");

$db_link->query("UPDATE employees SET pay_item='$pay_item' WHERE id = '$user_id'");

In my Database, ‘pay_item’, is VARCHAR(100), since I used to have it as a textfield, and the only two values I would want there are ‘Regular Hourly’ and ‘Regular Salary’. So I wanted to create a Drop Down where I can populate with those two options.

Any help would be highly appreciated! Thanks!

Advertisement

Answer

I am not sure if that is what you want

<td colspan="3">
    <select name="pay_item">

        <?php
        if($pay_item === "Regular Hourly"){
            echo "<option value='Regular Hourly' selected>Regular Hourly</option>";
            echo "<option value='Regular Salary'>Regular Salary</option>"
             }
         elseif($pay_item === "Regular Salary"){
             echo "<option value='Regular Salary' selected>Regular Salary</option>";
             echo "<option value='Regular Hourly'>Regular Hourly</option>";
             }
        

        ?>
        </select>
    </td>

Depending on the value of $pay_item the dropdown list will be generated

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement