kadams Posted July 9, 2010 Share Posted July 9, 2010 Hi There, I wonder if somebody could possibly help me. I have some code which basically should allow a user to enter a city in and it should display the prefixes from my database. If it cannot match the city the user has entered to what is in the database it should say it does not exist. The problem I have it that it does not tell me if it cannot find a city it still says "Cities found for: blah blah" and displays nothing when it should display an error message. My code is listed as below: <!CITY FINDER FORM > <html> <form action="finder1.php" method="get"> <fieldset> <legend>Enter your City</legend> <input type="text" name="userinputcity" /> <input type="submit" value="Search Cities"/> </fieldset> </form> <!POSTCODE FINDER FORM> <form action="finder1.php" method="get"> <fieldset> <legend>Enter your postcode prefix (S1,NG)</legend> <input type="text" name="userinputarea" /> <input type="submit" value="Search area's"/> </fieldset> </form> </html> <!COLLECTS THE USER'S INPUT AND PUTS IT INTO THE $USERINPUT VARIABLE> <?php $userinputcity = ""; ?> <?php $result = ""; ?> <?php $userinputcity = $_GET['userinputcity']; ?> <?php $checkprefix = "SELECT prefix from store WHERE city = '$userinputcity'"; //IF USER HAS PUT SOMETHING INTO TEXT BOX CHECK IT if($userinputcity){ print "Area's found for: " . $userinputcity; } else{ print "Enter City or area prefix to search"; } $result = mysql_query($checkprefix) or die(mysql_error()); while($row = mysql_fetch_array($result)){ if($row['city'] = '$userinputcity') { print "<br />"; print "<br />"; print "<br />"; echo $row['prefix']; } else { print "<br />"; print "Cities not found, sorry."; } } ?> Quote Link to comment Share on other sites More sharing options...
kumarkiranm Posted July 9, 2010 Share Posted July 9, 2010 Use a counter and display the error message out side the while loop. Because you dont have any rows round, control not entering into while loop to display error. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 Personally I would do something like: <?php if (isset($_POST['submit'])) { $userinputcity = $_GET['userinputcity']; if (empty($userinputcity)) { echo 'Please enter a city name.'; } else { $result = mysql_query(sprintf("SELECT prefix FROM store WHERE city = '%s'", $userinputcity)); if (mysql_num_rows($result) > 0) { echo 'Cities found for: ' . $userinputcity; while ($row = mysql_fetch_array($result)) { echo $row['prefix'] . '<br />'; } } else { echo 'No cities found.'; } } } ?> <!CITY FINDER FORM > <html> <form action="finder1.php" method="get"> <fieldset> <legend>Enter your City</legend> <input type="text" name="userinputcity" /> <input type="submit" value="Search Cities" name="submit" /> </fieldset> </form> <!POSTCODE FINDER FORM> <form action="finder1.php" method="get"> <fieldset> <legend>Enter your postcode prefix (S1,NG)</legend> <input type="text" name="userinputarea" /> <input type="submit" value="Search area's" name="submit" /> </fieldset> </form> </html> Quote Link to comment Share on other sites More sharing options...
kadams Posted July 9, 2010 Author Share Posted July 9, 2010 HI Wolphie, That worked a treat, thanks very much for your time and help Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 Please mark topic as SOLVED. Thank you. Quote Link to comment Share on other sites More sharing options...
kadams Posted July 12, 2010 Author Share Posted July 12, 2010 Hi Guys, Many thanks for your previous help with this question. I'd like to ask one final question if I may. At present the current set-up works, however there is 2 seperate boxes for a user to enter either a city in one box or a postcode prefix in another box. I'd like to have the 2 merged together so that a user can enter either of these things in the one text box and still get the same results taken from my database. Can anybody help me as to how I may do this, or provide me with the code to make this work? Any further help would be much appreciated. Thanks, Kyle 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.