Jump to content

Searching a MySQL Database from 2 Input boxes


grunshaw

Recommended Posts

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.