Hi all,
Not sure if this is the place to ask this, but I've been searching around for a few days here and not coming up with anything yet, so hopefully someone can point me in the right direction.
I have a database of approximately 300 companies. I need to create a drop down search menu so people can find records in a certain category. I want this menu to be able to pull the categories automatically from the database into the menu, then when they click on submit, the records/companies in that category are shown.
I found a couple of tutorials online on extracting database elements into the select form field, but the problem is when I click on submit, nothing happens.
Here are the two scripts/tutorials I tried, but they don't pull up any results for the categories that are pulled from the database, and I know there are records for those categories in the database. Am I missing the print results line somewhere?
#1: Here is the first tutorial I tried:
<HTML>
<HEAD>
<TITLE>Sample Form</TITLE>
</HEAD>
<BODY>
<?
// create connection
$connection = mysql_connect("localhost","xxxxxx","xxxxxx")
or die("Couldn't make connection.");
// select database
$db = mysql_select_db("members", $connection)
or die("Couldn't select database.");
// create SQL statement
$sql = "SELECT category FROM members
ORDER BY category ASC";
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection)
or die("Couldn't execute query.");
// put data into drop-down list box
while ($row = mysql_fetch_array($sql_result)) {
$category = $row["category"];
$option_block .= "<OPTION value=\"$category\">$category</OPTION>";
}
?>
<FORM method="post" action="test3.php">
<P>Category:<br>
<SELECT name="category">
<? echo "$option_block"; ?>
</SELECT>
<P><INPUT type="submit" value="submit"></p>
</FORM>
</BODY>
</HTML>
#2: This is the second tutorial I tried:
<HTML>
<BODY>
<script language="php">
// create connection
$connection = mysql_connect("localhost","xxxx","xxxxx")
or die("Couldn't make connection.");
// select database
$db = mysql_select_db("members", $connection)
or die("Couldn't select database.");
if($react == "search_category") {
if($category) {
$query = "SELECT category FROM members ORDER BY category ASC";
$result = mysql_query($query, $connection);
if(mysql_num_rows($result)) {
print("<strong>$category</strong><p>");
}
} else {
print("<strong>no records are found, sorry.</strong><p>");
}
}
elseif ($react == "search_alpha") {
if(($company)) {
$query = "SELECT * FROM members ORDER BY company LIMIT " . $start . ", 5";
$result = mysql_query($query, $connection);
} else {
print("<strong>no records are found, sorry.</strong><p>");
}
}
else {
print("<center>Search the MHHP Members Database</center>");
}
</script>
<form method="POST" action="testnew.php"><div align="center"><center><p>Search Categories
<input type="hidden" name="react" value="search_record">
<select name="category" size="1">
<script language="php">
$query = "SELECT category FROM members ORDER BY category";
$result = mysql_query($query, $connection);
if(mysql_num_rows($result)) {
// we have at least one category, so show all categories as options in select form
while($row = mysql_fetch_row($result))
{
print("<option value=\"$row[0]\">$row[0]</option>");
}
} else {
print("<option value=\"\">No records were found, sorry</option>");
}
</script>
</select><input type="submit" value="Submit"></p></center></div>
</form>
<form method="POST" action="testnew.php"><div align="center"><center><p>Search Alphabetically
<input type="hidden" name="react" value="search_alpha">
<select name="category" size="1">
<script language="php">
$query = "SELECT company FROM members ORDER BY company";
$result = mysql_query($query, $connection);
if(mysql_num_rows($result)) {
// we have at least one category, so show all category as options in select form
while($row = mysql_fetch_row($result))
{
print("<option value=\"$row[0]\">$row[0]</option>");
}
} else {
print("<option value=\"\">No records were found, sorry</option>");
}
</script>
</select><input type="submit" value="Submit"></p></center></div>
</form>
</BODY>
</HTML>
Thanks in advance.