View Full Version : How to alphabetize a dropbox from sql

02-21-2010, 11:17 PM

I really new to Php and I'm probably over my head, haha.

I'm trying to have a drop box from a SQL database that is organized in alphabetical order. Here is my code for php:

$sql_unit="SELECT UnitID, UnitName FROM unit";


while ($row=mysql_fetch_array($result_unit))
$unit_options.="<OPTION VALUE=\"$unitid\">".$unitname;

And the html code below it

<select name="remove_unit">
<option><?php echo $unit_options?></option>
<input type="button" value="Remove" />

But here is the thing, I need to be able to remove the selected item from the database. Which I don't know how to do....what how I need to structure this?


02-21-2010, 11:25 PM
Okay I solved the first part.

I changed
$sql_unit="SELECT UnitID, UnitName FROM unit";

$sql_unit="SELECT UnitID, UnitName FROM unit ORDER BY UnitName";

But...what information do I need to remove that selection from the database?

02-21-2010, 11:28 PM
if ( isset( $_POST['remove_unit'] ) )
$sql = 'DELETE FROM `unit` WHERE `UnitID` = ' . ( int ) $_POST['remove_unit'] . ' LIMIT 1';
mysql_query( $sql );

02-22-2010, 02:10 AM
thanks, I had to take out some (')s to make it work but it got the job done.

02-22-2010, 03:48 AM
Just sticking my nose in...

You shouldn't have had to remove any of the single quotes, as kbluhm's code was perfectly correct. You did notice the difference between the single quote and the `? - not sure what ` is called lol.

02-22-2010, 04:11 AM
Haha... good question. Backtick?

I was curious as to what needed removing as well.