cloudsurferuk Posted December 17, 2009 Share Posted December 17, 2009 Hi, I am using the following code to search and return flights from a database. The user searches by inputting a DepID and an ArrID and I want to display only flights that match both DepID and ArrID. However at the moment I am get results only from DepID.. Im sure its something simple but any help appreciated. <?php if(isset($_POST['submit'])){ if(isset($_GET['go'])){ if(preg_match("/^[ a-zA-Z]+/", $_POST['DepID'], $_POST['ArrID'])){ $DepID=$_POST['DepID']; $ArrID=$_POST['ArrID']; //connect to the database $db=mysql_connect ("localhost", "HIDDEN", "HIDDEN") or die ('I cannot connect to the database because: ' . mysql_error()); //-select the database to use $mydb=mysql_select_db("HIDDEN"); //-query the database table $sql="SELECT FlightNo, DepID, DepTime, ArrID, ArrTime, Equip FROM routes WHERE DepID LIKE '%" . $DepID . "%' OR ArrID LIKE '%" . $ArrID ."%'"; //-run the query against the mysql query function $result=mysql_query($sql); //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $FlightNo=$row['FlightNo']; $DepID=$row['DepID']; $DepTime=$row['DepTime']; $ArrID=$row['ArrID']; $ArrTime=$row['ArrTime']; $Equip=$row['Equip']; //-display the result of the array echo "<ul>\n"; echo "<li>" .$FlightNo . " " . $DepID . " " . $DepTime . " " . $ArrID . " " . $ArrTime . " " . $Equip . "</a></li>\n"; echo "</ul>"; } } else{ echo "<p>Please enter a search query</p>"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/185429-php-search-issue/ Share on other sites More sharing options...
.josh Posted December 17, 2009 Share Posted December 17, 2009 in your query, change the OR to AND Link to comment https://forums.phpfreaks.com/topic/185429-php-search-issue/#findComment-978906 Share on other sites More sharing options...
.josh Posted December 17, 2009 Share Posted December 17, 2009 also, your preg_match is wrong. preg_match("/^[ a-zA-Z]+/", $_POST['DepID'], $_POST['ArrID']) preg_match assigns results to what is specified in the 3rd argument. Link to comment https://forums.phpfreaks.com/topic/185429-php-search-issue/#findComment-978907 Share on other sites More sharing options...
roopurt18 Posted December 17, 2009 Share Posted December 17, 2009 Please enclose your code between [ code ] and [/ code ] tags, without the spaces, when posting. It makes it so much easier to help you. Link to comment https://forums.phpfreaks.com/topic/185429-php-search-issue/#findComment-978912 Share on other sites More sharing options...
cloudsurferuk Posted December 17, 2009 Author Share Posted December 17, 2009 thanks for the help guys, now i have changed the or to and but now i get nothing displayed at all. also what should preg_match be? <?php if(isset($_POST['submit'])){ if(isset($_GET['go'])){ if(preg_match("/^[ a-zA-Z]+/",$_POST['DepID'],$_POST['ArrID'])){ $DepID=$_POST['DepID']; $ArrID=$_POST['ArrID']; //connect to the database $db=mysql_connect ("localhost", "", "") or die ('I cannot connect to the database because: ' . mysql_error()); //-select the database to use $mydb=mysql_select_db(""); //-query the database table $sql="SELECT DepID, DepTime, ArrID, ArrTime, Equip, FlightNo FROM routes WHERE DepID LIKE '%" . $DepID . "%' AND ArrID LIKE '%" . $ArrID ."%'"; //-run the query against the mysql query function $result=mysql_query($sql); //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $DepID=$row['DepID']; $DepTime=$row['DepTime']; $ArrID=$row['ArrID']; $ArrTime=$row['ArrTime']; $Equip=$row['Equip']; $FlightNo=$row['FlightNo']; //-display the result of the array echo "<ul>\n"; echo "<li>" . $DepID . " " . $DepTime . " " . $ArrID . " " . $ArrTime . " " . $Equip . " " .$FlightNo . "</a></li>\n"; echo "</ul>"; } } else{ echo "<p>Please enter a search query</p>"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/185429-php-search-issue/#findComment-978929 Share on other sites More sharing options...
.josh Posted December 17, 2009 Share Posted December 17, 2009 I suspect what you really want for the condition is this: if(preg_match("/^[ a-zA-Z]+/",$_POST['DepID'])&&preg_match("/^[ a-zA-Z]+/",$_POST['ArrID'])){ Link to comment https://forums.phpfreaks.com/topic/185429-php-search-issue/#findComment-979151 Share on other sites More sharing options...
cloudsurferuk Posted December 17, 2009 Author Share Posted December 17, 2009 You're a star, that sorted it. my learning of PHP marches ever onward. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/185429-php-search-issue/#findComment-979168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.