I am allowing users insert data into a database using a form.
I want one of the fields "department" to display all existing departments in the database in a combo box for users to choose from and also have a field in the combo box for them to add a new department.
Is there a way for me to do this in using php?
I am using php to access the database
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
HTML forms don't have an option for a true combo box (one with a drop-down list and a field to enter a value not in the list) but you can approximate this behavior with two separate input fields (a select input and a text input).
Do a google on "html combo box" for implementation ideas. (here's one)
As for the PHP side of things, you'll need to populate the list box based on the results of a query when the page loads. The query would be something like "select distinct(dept_value) from mytable".
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Quote:
the distinct is not working.any other reason why the same value in the database is appearing twice in the combo?
You'd have to post examples of your data and query, because "distinct" does in fact work for everyone else in the free world. More than likely you have different capitalization, white space (\n, \t, \r, etc) or other dirty data that is listing more unique values than you expect.
Quote:
is it possible to have a field in the combo for adding a new value to the database or not?
So did you google it like I suggested? In a few seconds I found three different websites with methods of doing that.