...

View Full Version : How to alphabetize a dropbox from sql



metomeya
02-21-2010, 11:17 PM
Hi,

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";
$result_unit=mysql_query($sql_unit);

$unit_options="";

while ($row=mysql_fetch_array($result_unit))
{
$unitid=$row["UnitID"];
$unitname=$row["UnitName"];
$unit_options.="<OPTION VALUE=\"$unitid\">".$unitname;
}


And the html code below it



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


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?

Thanks!

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

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

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


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

kbluhm
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 );
}

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

Taylor_1978
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.

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

I was curious as to what needed removing as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum