Forum Moderators: coopster

Message Too Old, No Replies

dropdown box query sql database HELP!

         

mike_tw

1:14 pm on Apr 3, 2007 (gmt 0)

10+ Year Member



ello there!

I'm a noob at this you'll no doubt notice

As youll see, you can sort the database by manufacturer, color etc in a way that you can select colour from a particular manufcaturer too.

They use java servlets, i want to use php + mysql.

I have a database in place... i have built a search.htm form with four drop down menus.

This is the results.php page which i know is wrong and doesnt work.

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");

function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc())
{
$Manufacturer = $_POST['Manufacturer'];
}
else
{
$Manufacturer = addslashes($_POST['Manufacturer']);
}
$Manufacturer = secured($Manufacturer);

if ($Manufacturer!= "SelectManufacturer") //The Default select value
{
$query = "SELECT * FROM bricks WHERE Manufacturer=$Manufacturer1";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if(!$num_rows)
{
echo "<table><tr><td align='center'>No data found 0 record returned</td></tr></table>";
}
else
{
echo "<table align='center'><tr>";
echo "<td align='center'><b>Manufacturer</b></td>";
echo "<td align='center'><b>Colour</b></td>";
echo "<td align='center'><b>Texture</b></td>";
echo "<td align='center'><b>Manufacturing Process</b></td>";
echo "</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
echo "<tr><td align='center'>".$row["Manufacturer"]."</td>";
echo "<td align='center'>".$row["Colour"]."</td>";
echo "<td align='center'>".$row["Texture"]."</td>";
echo "<td align='center'>".$row["Manufacturing Process"]."</td>";
echo "</tr>";

}
echo "</table>";
}
?>

What i need help with is linking the selection from the drop down menu(s) and running a query if they are selected by pressing submit.

I have four drop downs .. Manufacturer.. colour...texture...manufacturing process

Any help appreciated

[edited by: jatar_k at 2:07 pm (utc) on April 3, 2007]
[edit reason] no urls thanks [/edit]

jatar_k

3:11 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld mike_tw,

maybe this thread will help
Help developing MySQL search query [webmasterworld.com]

mcibor

9:02 am on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like to use 4 ifs, but that's completely up to you.
After making variables secure I would write:

$query = "SELECT * FROM bricks WHERE id > '0'";

if($Manufacturer) $query .= " AND Manufacturer='$Manufacturer'";
if($Colour) $query .= " AND Colour='$Colour'";
if($Texture) $query .= " AND Texture='$Texture'";
if($Manufacturing_Process) $query .= " AND Manufacturing_Process='$Manufacturing_Process'";

//$query .= " LIMIT $from_record $how_many_records"; if you want to add pagination

Remember to put ' ' around strings, and use mysql_real_escape_string to make string mysql secure.

Regards
Michal