monkeypaw201 Posted July 15, 2008 Share Posted July 15, 2008 So i have a form that posts to a page and i want the page to filter the results if needed.... i've posted all the code below. Thanks! Form (Works) <form id="search" method="get" action="schedule.php"> <table width="100%" border="0"> <tr> <td><div align="left"><span style="color:#BBB; font-size:75%;">Aircraft</span><br /> </div> <label> <div align="left"> <select name="aircraft" id="aircraft"> <option value="%">Any</option> <?php $aircraft = mysql_query("SELECT * FROM `aircraft` ORDER BY `name` ASC"); while($row_aircraft = mysql_fetch_array($aircraft)) { ?> <option value="<?php echo $row_aircraft['id']; ?>"><?php echo $row_aircraft['name']; ?></option> <?php } ?> </select> </div> </label> </td> <td><div align="right"><span style="color:#BBB; font-size:75%;">Departure</span><br /> </div> <label> <div align="right"> <select name="from" id="from"> <option value="%">Any</option> <?php $airports = mysql_query("SELECT * FROM `airports` ORDER BY `city` ASC"); while($row_airports = mysql_fetch_array($airports)) { ?> <option value="<?php echo $row_airports['id']; ?>"><?php echo $row_airports['city']; ?> (<?php echo $row_airports['icao']; ?>)</option> <?php } ?> </select> </div> </label> </td> </tr> <tr> <td><div align="left"><span style="color:#BBB; font-size:75%;">Flight Type</span><br /> <label> <select name="type" id="type"> <option value="%">Any</option> <option value="Regional">Regional</option> <option value="Short-Haul">Short-Haul</option> <option value="Medium-Haul">Medium-Haul</option> <option value="Long-Haul">Long-Haul</option> </select> </label> </div></td> <td><div align="right"><span style="color:#BBB; font-size:75%;">Arrival</span><br /> </div> <label> <div align="right"> <select name="to" id="to"> <option value="%">Any</option> <?php $airports = mysql_query("SELECT * FROM `airports` ORDER BY `city` ASC"); while($row_airports = mysql_fetch_array($airports)) { ?> <option value="<?php echo $row_airports['id']; ?>"><?php echo $row_airports['city']; ?> (<?php echo $row_airports['icao']; ?>)</option> <?php } ?> </select> </div> </label> </td> </tr> </table> <p align="center"> <label> <input type="submit" name="button" id="button" value="Search Flights" /> </label> </p> </form> Search Page (Error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" ) <?php //relevant code $routes = mysql_query("SELECT * FROM `routes` WHERE aircraft = '$_GET[aircraft]' AND from = '$_GET[from]' AND type = '$_GET[to]' AND to = '$_GET[type]' ORDER BY RAND()"); while($row_routes = mysql_fetch_array($routes)) { ?> <?php //Farther down the page } ?> Link to comment https://forums.phpfreaks.com/topic/114914-search-form-output-help/ Share on other sites More sharing options...
p2grace Posted July 16, 2008 Share Posted July 16, 2008 Try echoing the query and make sure it looks correct, and if it doesn't echo the mysql_error(). You should always setup error-handling on your queries before attempting to use data from them. You also need to put {} around get variables in that query so they can be interpreted. You also should clean the get variables before using them in a query to avoid sql injection. This would be a plausible solution: <?php //relevant code $aircract = trim(mysql_real_escape_string($_GET['aircraft'])); $from = trim(mysql_real_escape_string($_GET['from'])); $to = trim(mysql_real_escape_string($_GET['to'])); $type = trim(mysql_real_escape_string($_GET['type'])); $routes = mysql_query("SELECT * FROM `routes` WHERE aircraft = '$aircraft' AND from = '$from' AND type = '$to' AND to = '$type' ORDER BY RAND()"); if($routes){ while($row_routes = mysql_fetch_array($routes)) { ?> <?php //Farther down the page } }else{ echo "mysql error".mysql_error(); } ?> Link to comment https://forums.phpfreaks.com/topic/114914-search-form-output-help/#findComment-591162 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.