grunshaw Posted August 8, 2009 Share Posted August 8, 2009 Right-ey-o! This is the last one for now! :-) With the mobility system, users need to search for routes, for example FROM front door TO back door. |n the table, there are 2 columns "ent" for enterance and "exit" for the finish point. I have a form with 2 input boexs names "from" and "to". I have the script that grabs the inputed data $from=$_POST['from']; $to=$_POST['to'] Now the SQL is where i'm stuck. I can get it to query one of the columns fine, but i'm not too sure how to get it to query both and ignore one of them if $to or $from is blank. I got as far as $query="SELECT * FROM routes WHERE ent='$from'"; $result=mysql_query($query); $num=mysql_numrows($result) I've tried AND and OR but doesn't work. Any suggestions? James. Link to comment https://forums.phpfreaks.com/topic/169352-searching-a-mysql-database-from-2-input-boxes/ Share on other sites More sharing options...
TeNDoLLA Posted August 8, 2009 Share Posted August 8, 2009 Try this <?php $from = '%' . $_POST['from'] . '%'; $to = '%' . $_POST['to'] . '%'; $query="SELECT * FROM routes WHERE ent LIKE '$from' AND exit LIKE '$to'"; $result=mysql_query($query); $num=mysql_numrows($result) Link to comment https://forums.phpfreaks.com/topic/169352-searching-a-mysql-database-from-2-input-boxes/#findComment-893653 Share on other sites More sharing options...
grunshaw Posted August 8, 2009 Author Share Posted August 8, 2009 I had already tried that. My code is as follows: <? $from=$_POST['from']; $to=$_POST['to']; echo "<h4>You searched for $from to $to</h4>"; include ("db_connect.php"); $query="SELECT * FROM routes WHERE ent LIKE '%$from%' AND exit LIKE '%$to%'"; $result=mysql_query ($query); $num=mysql_numrows ($result); if($num == 0){ echo "<h4>There are 0 Routes found</h4>"; } elseif($num == 1){ echo "<h4>There is 1 Result matching your query</h4>"; } else { echo "<h4>There are $num Results matching your query</h4>"; } $i=0; while($i < $num){ $rid=mysql_result ($result, $i, "rid"); $ent=mysql_result ($result, $i, "ent"); $exit=mysql_result ($result, $i, "exit"); $steps=mysql_result ($result, $i, "steps"); $desc=mysql_result ($result, $i, "description"); $memo=mysql_result ($result, $i, "memo"); echo "<li><a href='#'> $ent to $exit</a><br>This route contains $steps steps</li>"; $i++; } ?> Using that code, the search facility does not seem to display any results. The criterea i'm using are "front door" and "back door" so technically, if I type in "front" to form for $from and type in "back" for the $to, it should come up - it doesn't. I would like it so that I can type in either or both the boees. Any other ideas? Link to comment https://forums.phpfreaks.com/topic/169352-searching-a-mysql-database-from-2-input-boxes/#findComment-893690 Share on other sites More sharing options...
grunshaw Posted August 9, 2009 Author Share Posted August 9, 2009 I'm still having problems with this, i'm thinking it might be neccessary to do a nested query perhaps? Does anybody have any suggestions on how to query a database with 2 or more variables using 2 input boxes! Best James. Link to comment https://forums.phpfreaks.com/topic/169352-searching-a-mysql-database-from-2-input-boxes/#findComment-894171 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.