简体   繁体   English

<select>有两个不同的选项不是来自数据库,而是自定义文本 php mysql, html

[英]<select> 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'.我在 phpmyadmin 中有我的数据库,我有一个表“员工”。 One of the fields is 'pay_item', and this particular field is type 'varchar(100)'.其中一个字段是“pay_item”,这个特定字段的类型是“varchar(100)”。

I want to have a dropdown in my php website, and I want it to have two different options: 'Hourly' and 'Salary'.我想在我的 php 网站中有一个下拉菜单,我希望它有两个不同的选项:“每小时”和“工资”。

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.我已经让它从数据库中检索值,因此无论该员工选择了什么,它都不会出现在屏幕上,并且将在下拉列表中选择“pay_item”。

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'我知道如何使用“while”循环进行编写,并从数据库填充,但在这种情况下,我只想拥有这两个选项,而不是使用一段时间,因为“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 .现在,我忘了提到,当它在“Regular Salary”上时,它确实会将更改更改为“Regular Hourly”,而不是相反,所以我认为 .

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.以前,我将此作为文本字段,因此人们输入“Regular Hourly”等,但有时他们会输入语法错误,因此我试图通过下拉列表来避免这种情况。

The way I am sending this to the DB, is with a normal UPDATE or INSERT query.我将其发送到数据库的方式是使用正常的 UPDATE 或 INSERT 查询。

$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'.在我的数据库中,'pay_item' 是 VARCHAR(100),因为我曾经将它作为一个文本字段,而我想要的仅有的两个值是“Regular Hourly”和“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!谢谢!

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根据 $pay_item 的值,将生成下拉列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM