Page 1 of 1

Getting database entries from a combo box?

Posted: Thu Oct 05, 2006 8:57 pm
by cturner
Can someone please tell me how I can get database entries from a combo box? Thanks in advance.

Here is some code that I am working with:

Code: Select all

require "config_admin.php";
	echo "View products in: ";
	$sql2 = mysql_query("SELECT * FROM tbl_category") or die ("Could not select the database because: " . 	mysql_error());
	// display the categories combo box
	while($row = mysql_fetch_array($sql2)){
		echo "<select name=\"selectCategory\" id=\"selectCategory\" class=\"select\">";
		echo "<option value=\"all\" selected>All Categories</option>";
		echo "<option value=".$row[category].">".$row[category_name]."</option>";
		echo "</select>";
	}

Posted: Thu Oct 05, 2006 9:00 pm
by Luke
:?: Try asking that again... except this time... elaborate.

Posted: Thu Oct 05, 2006 9:16 pm
by waradmin
Well, if I understand correctly you just need to complete the <form> tag's, and have the value's passed by say $_POST to another file, or even the same file and have a database query execute using the POST values.

Posted: Thu Oct 05, 2006 9:35 pm
by cturner
After adding data to a database and displaying the data in a combo box and when the user selects a entry from the combo box I am wanting that entry to do something. For example display a certain amount of products from another database table.

Posted: Thu Oct 05, 2006 10:16 pm
by Flamie
your logic is partially right, but the order of your commands isnt.
Try this:

Code: Select all

echo "<select name=\"selectCategory\" id=\"selectCategory\" class=\"select\">";
echo "<select name=\"selectCategory\" id=\"selectCategory\" class=\"select\">";
$result = mysql_query("your query here");
while($row = mysql_fetch_array($result)
{
echo "<option value=".$row[category].">".$row[category_name]."</option>";
}
echo "</select>";
You dont wanna create and close your select inside the while, thats not good :)