zang8027 Posted January 13, 2009 Share Posted January 13, 2009 So i have a page with 3 drop down menus. Each menu's values are recieved from a database. When you select the first value, it reloads the page and displays the proper values in the dropdown below it. Then that happens again. The problem I am having is that because my drop downs are not getting values until the one above it is selected, I am getting: Warning: mysql_fetch_array(): supplied argument is not a valid MYSQL result. Is there like a if(null) statement I can use to set the default value to something like "Please Select a State"? <FORM action="findrestaurants.php" method="POST"> <SELECT NAME="stateList" onChange="this.form.submit()"> <OPTION>Please Select a State <?php //Make a connection to the server... connect("servername","username","password") $link=mysql_connect("localhost","root") or die("No server connection".mysql_error()); //connect to our database $db=mysql_select_db("dineonline_db") or die("No Database Connection ".mysql_error()); //construct a SQL query that shows all hotels in the city $query="SELECT * FROM states"; //run the query $result=mysql_query($query); //for each row returned by our query, do what is in curly braces while($row = mysql_fetch_array($result)){ //store into a variable the ID # for the current row $stateID=$row['state_ID']; //store into a variable the Name for the current row $stateName=$row['state_name']; //Print out each "<OPTION> with the proper values print "<OPTION VALUE='$stateID'>$stateName"; } //close the connection mysql_close($link); ?> </SELECT> </FORM> <!-- Below is the cityBig List drop down code --> <FORM action="findrestaurants.php" method="POST"> <SELECT NAME="cityLargeList" onChange="this.form.submit()"> <OPTION>Please Select an Area <?php //Get the value from City Large $userState = $_POST['stateList']; //Make a connection to the server... connect("servername","username","password") $link=mysql_connect("localhost","root") or die("No server connection".mysql_error()); //connect to our database $db=mysql_select_db("dineonline_db") or die("No Database Connection ".mysql_error()); //construct a SQL query that shows all hotels in the city $query="SELECT * FROM cityLarge WHERE state_ID = $userState"; //run the query $result=mysql_query($query); //for each row returned by our query, do what is in curly braces while($row = mysql_fetch_array($result)){ //store into a variable the ID # for the current row $cityLargeID=$row['cityLarge_ID']; //store into a variable the Name for the current row $cityLargeName=$row['cityLarge_name']; //Print out each "<OPTION> with the proper values print "<OPTION VALUE='$cityLargeID'>$cityLargeName"; } //close the connection mysql_close($link); ?> </SELECT> </FORM> <!-- Below is the citySmall List drop down code --> <FORM action="displayRest.php" method="get"> <SELECT NAME="citySmallList"> <OPTION>Please Select a City <?php //Get the value from City Large $userCityLarge = $_POST['cityLargeList']; //Make a connection to the server... connect("servername","username","password") $link=mysql_connect("localhost","root") or die("No server connection".mysql_error()); //connect to our database $db=mysql_select_db("dineonline_db") or die("No Database Connection ".mysql_error()); //construct a SQL query that shows all hotels in the city $query="SELECT * FROM citySmall WHERE cityLarge_ID = $userCityLarge"; //run the query $result=mysql_query($query); //for each row returned by our query, do what is in curly braces while($row = mysql_fetch_array($result)){ //store into a variable the ID # for the current row $citySmallID=$row['citySmall_ID']; //store into a variable the Name for the current row $citySmallName=$row['citySmall_name']; //Print out each "<OPTION> with the proper values print "<OPTION VALUE='$citySmallID'>$citySmallName"; } //close the connection mysql_close($link); ?> </SELECT> <br/> <br/> <input type="submit" value="Find restaurants" /> </FORM> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 13, 2009 Share Posted January 13, 2009 The correct approach would be to check to see if those values actually exist in the $_POST array (using isset) and only execute the query if they do. Quote Link to comment Share on other sites More sharing options...
zang8027 Posted January 13, 2009 Author Share Posted January 13, 2009 didn't even think of that. Would i do like.. if(isset($_POST['myvar'])) { PHP } Quote Link to comment Share on other sites More sharing options...
zang8027 Posted January 13, 2009 Author Share Posted January 13, 2009 Now i added this: <?php //Get the value from City Large $userState = $_POST['stateList']; if(isset($userState)) { //Make a connection to the server... connect("servername","username","password") $link=mysql_connect("localhost","root") or die("No server connection".mysql_error()); //connect to our database $db=mysql_select_db("dineonline_db") or die("No Database Connection ".mysql_error()); //construct a SQL query that shows all hotels in the city $query="SELECT * FROM cityLarge WHERE state_ID = $userState"; //run the query $result=mysql_query($query); //for each row returned by our query, do what is in curly braces while($row = mysql_fetch_array($result)){ //store into a variable the ID # for the current row $cityLargeID=$row['cityLarge_ID']; //store into a variable the Name for the current row $cityLargeName=$row['cityLarge_name']; //Print out each "<OPTION> with the proper values print "<OPTION VALUE='$cityLargeID'>$cityLargeName"; }else{ } } //close the connection mysql_close($link); ?> And i get Unexpected T_ELSE on line 76. If i delete the else part, i get the drop down boxes again but i get: mysql_close():2 is not a valid MYSQL-Link Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 15, 2009 Share Posted January 15, 2009 You're while statement is closed by the brace on this line: }else{ Evidently you intended to close the while loop first. If you indented your code properly, it'd make things like that a whole stack easier to spot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.