Jump to content

Query database using 3 dropdown menus


Pitot-Tube
 Share

Recommended Posts

Hi there, I am new to PHP and need some help! I am trying to setup a form where customers can use 3 dropdown menus and recieve back products from a database. I have no problem connecting to the database and recieving specified data from 1 dropdown menu, but i need the query to be based on the values selected from all 3 menus. In the end I need customers to select their specific - Treadwidth / Profile / Diameter from the lists and recieve the all the tires having that specific size. The Treadwidth menu is working, but the Profile / Diameter are having no impact on the search. Here is the code I have so far(remember I am a newbe!):<?php require_once('Connections/tires_db.php'); ?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue;}}$colname_Recordset1 = "-1";if (isset($_POST['treadwidth'])) { $colname_Recordset1 = $_POST['treadwidth'];}mysql_select_db($database_tires_db, $tires_db);$query_Recordset1 = sprintf("SELECT * FROM tireinfo WHERE treadwidth = %s", GetSQLValueString($colname_Recordset1, "text"));$Recordset1 = mysql_query($query_Recordset1, $tires_db) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);$totalRows_Recordset1 = mysql_num_rows($Recordset1);mysql_select_db($database_tires_db, $tires_db);$query_Recordset1 =("SELECT * FROM tireinfo");$Recordset1 = mysql_query($query_Recordset1, $tires_db) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);$totalRows_Recordset1 = mysql_num_rows($Recordset1);$colname_Recordset1 = "-1";if (isset($_POST['treadwidth']))if (isset($_POST['profile']))if (isset($_POST['diameter'])) { $colname_Recordset1 = $_POST['treadwidth']; $colname_Recordset1 = $_POST['profile']; $colname_Recordset1 = $_POST['diameter'];}// Connect databasemysql_connect("localhost","root","");mysql_select_db("tires_db"); // If submitted, check the value of "select". If its not blank value, get the value and put it into $select.if(isset($select)&&$select!="treadwidth")if(isset($select2)&&$select2!="profile")if(isset($select3)&&$select3!="diameter"){$select=$_POST['treadwidth'];$select2=$_POST['profile'];$select3=$_POST['diameter'];}?><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head><body><form id="form1" name="form1" method="post" action="results1.php">Treadwidth :<select name="treadwidth" size="1"> <option value="" selected="selected" <?php if (!(strcmp("", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>--- Treadwidth ---</option> <option value="145" <?php if (!(strcmp(145, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>145</option> <option value="155" <?php if (!(strcmp(155, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>155</option> <option value="165" <?php if (!(strcmp(165, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>165</option><option value="175" <?php if (!(strcmp(175, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>175</option> <option selected value="185" <?php if (!(strcmp(185, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>185</option> <option value="195" <?php if (!(strcmp(195, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>195</option> <option value="205" <?php if (!(strcmp(205, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>205</option> <option value="215" <?php if (!(strcmp(215, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>215</option> <option value="225" <?php if (!(strcmp(225, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>225</option> <option value="235" <?php if (!(strcmp(235, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>235</option> <option value="245" <?php if (!(strcmp(245, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>245</option> <option value="255" <?php if (!(strcmp(255, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>255</option> <option value="265" <?php if (!(strcmp(265, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>265</option> <option value="275" <?php if (!(strcmp(275, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>275</option> <option value="285" <?php if (!(strcmp(285, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>285</option> <option value="295" <?php if (!(strcmp(295, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>295</option> <option value="305" <?php if (!(strcmp(305, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>305</option> <option value="315" <?php if (!(strcmp(315, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>315</option> <option value="325" <?php if (!(strcmp(325, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>325</option> <option value="335" <?php if (!(strcmp(335, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>335</option> <option value="345" <?php if (!(strcmp(345, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>345</option> <option value="355" <?php if (!(strcmp(355, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>355</option><option value="27x" <?php if (!(strcmp("27x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>27x</option> <option value="30x" <?php if (!(strcmp("30x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>30x</option> <option value="31x" <?php if (!(strcmp("31x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>31x</option> <option value="32x" <?php if (!(strcmp("32x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>32x</option> <option value="33x" <?php if (!(strcmp("33x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>33x</option> <option value="35x" <?php if (!(strcmp("35x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>35x</option> <option value="37x" <?php if (!(strcmp("37x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>37x</option> <option value="38x" <?php if (!(strcmp("38x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>38x</option> <option value="40x" <?php if (!(strcmp("40x", $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>40x</option> <option value="7.50" <?php if (!(strcmp(7.50, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>7.50</option> <option value="8.00" <?php if (!(strcmp(8.00, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>8.00</option> <option value="8.75" <?php if (!(strcmp(8.75, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>8.75</option> <option value="9.50" <?php if (!(strcmp(9.50, $row_Recordset1['treadwidth']))) {echo "selected=\"selected\"";} ?>>9.50</option> <?phpdo { ?> <?php} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) { mysql_data_seek($Recordset1, 0); $row_Recordset1 = mysql_fetch_assoc($Recordset1); }?> </select>Profile: <select name="profile" size="1"> <option value="" selected="selected" <?php if (!(strcmp("", $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>--- Profile ---</option> <option value="95" <?php if (!(strcmp(95, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>95</option> <option value="90" <?php if (!(strcmp(90, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>90 </option> <option value="85" <?php if (!(strcmp(85, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>85 </option> <option value="80" <?php if (!(strcmp(80, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>80 </option> <option value="75" <?php if (!(strcmp(75, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>75 </option> <option value="70" <?php if (!(strcmp(70, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>70 </option> <option selected value="65" <?php if (!(strcmp(65, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>65 </option> <option value="60" <?php if (!(strcmp(60, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>60 </option> <option value="55" <?php if (!(strcmp(55, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>55 </option> <option value="50" <?php if (!(strcmp(50, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>50 </option> <option value="45" <?php if (!(strcmp(45, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>45 </option> <option value="40" <?php if (!(strcmp(40, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>40 </option> <option value="35" <?php if (!(strcmp(35, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>35 </option><option value="30" <?php if (!(strcmp(30, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>30 </option> <option value="25" <?php if (!(strcmp(25, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>25 </option> <option value="8.5" <?php if (!(strcmp(8.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>8.5 </option> <option value="9.5" <?php if (!(strcmp(9.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>9.5 </option> <option value="10.5" <?php if (!(strcmp(10.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>10.5 </option> <option value="11.5" <?php if (!(strcmp(11.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>11.5 </option> <option value="12.5" <?php if (!(strcmp(12.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>12.5 </option> <option value="13.5" <?php if (!(strcmp(13.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>13.5 </option> <option value="14.5" <?php if (!(strcmp(14.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>14.5 </option> <option value="15.5" <?php if (!(strcmp(15.5, $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>15.5 </option> <option value="R" <?php if (!(strcmp("R", $row_Recordset1['profile']))) {echo "selected=\"selected\"";} ?>>R </option> <?phpdo { ?> <?php} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) { mysql_data_seek($Recordset1, 0); $row_Recordset1 = mysql_fetch_assoc($Recordset1); }?> </select> Diameter: <select name="diameter" size="1"> <option value="" selected="selected" <?php if (!(strcmp("", $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>--- Diameter ---</option> <option value="12" <?php if (!(strcmp(12, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>12"</option> <option value="13" <?php if (!(strcmp(13, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>13" </option> <option value="14" <?php if (!(strcmp(14, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>14" </option> <option selected value="15" <?php if (!(strcmp(15, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>15" </option> <option value="16" <?php if (!(strcmp(16, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>16" </option> <option value="16.5" <?php if (!(strcmp(16.5, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>16.5" </option> <option value="17" <?php if (!(strcmp(17, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>17" </option> <option value="17.5" <?php if (!(strcmp(17.5, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>17.5" </option> <option value="18" <?php if (!(strcmp(18, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>18" </option> <option value="19" <?php if (!(strcmp(19, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>19" </option> <option value="19.5" <?php if (!(strcmp(19.5, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>19.5" </option> <option value="20" <?php if (!(strcmp(20, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>20" </option> <option value="21" <?php if (!(strcmp(21, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>21" </option> <option value="22" <?php if (!(strcmp(22, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>22" </option> <option value="23" <?php if (!(strcmp(23, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>23" </option> <option value="24" <?php if (!(strcmp(24, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>24" </option> <option value="25" <?php if (!(strcmp(25, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>25" </option> <option value="26" <?php if (!(strcmp(26, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>26"</option> <option value="28" <?php if (!(strcmp(28, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>28" </option><option value="30" <?php if (!(strcmp(30, $row_Recordset1['diameter']))) {echo "selected=\"selected\"";} ?>>30" </option><?phpdo { ?><?php} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) { mysql_data_seek($Recordset1, 0); $row_Recordset1 = mysql_fetch_assoc($Recordset1); }?> </select> <?phpdo { ?> <?php} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) { mysql_data_seek($Recordset1, 0); $row_Recordset1 = mysql_fetch_assoc($Recordset1); }?> </select> <?php// Get records from database (table "name_list").$select=mysql_query("SELECT * FROM tireinfo");$select2=mysql_query("SELECT * FROM tireinfo");$select3=mysql_query("SELECT * FROM tireinfo");// Show records by while loop.while($row_list=mysql_fetch_assoc($select))while($row_list=mysql_fetch_assoc($select2))while($row_list=mysql_fetch_assoc($select3)){?><?php// End while loop.}?></select> <input type="submit" name="Submit" value="Search Tires" /></form><hr><p><?php// If you have selected from list box.if(isset($select)&&$select!="treadwidth")if(isset($select2)&&$select2!="profile")if(isset($select3)&&$select3!="diameter"){// Get records from database (table "name_list").$result=mysql_query("select * from tireinfo");$row=mysql_fetch_assoc($Recordset1);?><?php// End if statement. }// Close database connection.mysql_close();?></p></body></html><?phpmysql_free_result($Recordset1);?>

Link to comment
Share on other sites

are you verifying that the values a user selects are actually being sent to the script? You say one drop down works, and is that because you verified that their selection from the first drop down is being sent to the script via the form? If so, you should do the same with the rest, so that if you added this to the top of the script:

var_dump($_POST);

you should see the values you selected. after that it's a matter of verifying that they are making their way to the query correctly.

Link to comment
Share on other sites

right, but did you submit the form to actually populate the $_POST array first? Nothing will be in it the first time you land on the page.

Link to comment
Share on other sites

Ok, I now have this coming up, array(4) { ["treadwidth"]=> string(3) "215" ["profile"]=> string(2) "50" ["diameter"]=> string(2) "17" ["Submit"]=> string(12) "Search Tires" }but the problem I have is that when I submit this 215/50/17 in the dropdown menus, I am get tires that 215/45/16, etc. The only query that seems to be working is the treadwidth menu. How can I get the query to send all 3 values, and if any of them is missing it will send a error.

Link to comment
Share on other sites

If I add AND to the select statement will it work?Old:$query_Recordset1 = sprintf("SELECT * FROM tireinfo WHERE treadwidth = %s", GetSQLValueString($colname_Recordset1, "text"));New:$query_Recordset1 = sprintf("SELECT * FROM tireinfo WHERE treadwidth = 'treadwidth' AND profile= 'profile' AND diameter= 'diameter'");

Link to comment
Share on other sites

what happens when you try it? Are you shouldn't be trying with php variables instead?

WHERE treadwidth = $treadwidth

Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...