Taken from the plant problem guide:
Fungus gnats like to feed on roots of the plants and organic matter. Adults and larvae live in moist, shady areas. The adults lay there eggs on top of the soil, near the base of the stem and takes about 4 days to hatch. The larvae will start by eating the root hairs of the plant then working their way up the plant, Fungus gnats like to eat organic matter so they will be stealing away nutrients from your plants, so its best to get rid of them completely.
If plants are outdoors, check the soil of plants before taking them back indoors for adult gnats or their larvae.
Prevent indoor entry of gnats by making sure there is no open windows open without screens on. Aug is a bad time for them as they are worst that time of the year.
Put sticky traps on the soil surface to trap the gnats
Put potato slices on the surface of the soil. The larvae like it and will be drawn to it.. After about 4 to 5 days, remove the potato slices with the larvae. To get rid of them you can do a lot of things like either use a NO pest strip, neem oil or putting sand on the surface of the soil will suffocate the eggs and get rid of them as well. Tobacco juice kills them, and works well for re-occurrences!
They can be in or on the soil and can fly. In order to get rid of them you can use neem oil, sand on the surface again kills them, and no pest strips catch the ones that fly. A chemical product called Zone works very well and is very powerful and works well in hydro!
Other Products which can be used in hydro and soil are:
Chemicals
Hot Pepper Wax,Safer Yard & Garden Insecticide (which can be used right to the day of harvest),GNATROL( used in hydro in the water as well as soil),Doc's Neem Pest Soap,Safer Sticky Stakes,TR-11000 Pyrethrum.