Jump to content

Help With my Search Engine


anshuverma1989

Recommended Posts

hey guys i made this search engine.. it is actually working but not giving the proper result..

can u please tell me my mistake..  :P

 

<form method="get" action="person_search_result.php">
First Name: <input type="text" name="first_name" id="first_name">
Last Name: <input type="text" name="last_name" id="last_name">
State: <input type="text" name="state" id="state">
City / Area / Postal Code: <input type="text" name="city" id="city">

<input class="button" src="images/button_eng.gif" value="Search" tabindex="3" alt="Find" type="image">
</form>

 

thats my form code..

 

$fname = $_GET['first_name'];
$lname = $_GET['last_name'];
$state = $_GET['state'];
$city = $_GET['city'];

if($fname=="" AND $lname=="" AND $state=="" AND $city=="")
{
print "<script>";
    print "self.location='index.php';"; // Comment this line if you don't want to redirect
    print "</script>";
}	
else if($fname=="") 
{
$sql = mysql_query("SELECT * FROM person where last_name LIKE '%$lname%' AND state LIKE '%$state%' AND (address LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where last_name LIKE '%$lname%' AND state LIKE '%$state%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "Last Name: ".$_GET['last_name']."     State: ".$_GET['state']."     City / Area / Postal Code: ".$_GET['city'];
}

else if($lname=="") 
{
$sql = mysql_query("SELECT * FROM person WHERE first_name LIKE '%$fname%' AND state LIKE '%$state%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person WHERE first_name LIKE '%$fname%' AND state LIKE '%$state%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "First Name: ".$_GET['first_name']."     State: ".$_GET['state']."     City / Area / Postal Code: ".$_GET['city'];
}
else if($state=="") 
{
$sql = mysql_query("SELECT * FROM person where first_name LIKE '%$fname%' AND last_name LIKE '%$lname%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where first_name LIKE '%$fname%' AND last_name LIKE '%$lname%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "First Name: ".$_GET['first_name']."     Last Name: ".$_GET['last_name']."     City / Area / Postal Code: ".$_GET['city'];
}
else if($city=="") 
{
$sql = mysql_query("SELECT * FROM person where first_name LIKE '%$fname%' AND last_name LIKE '%$lname%' AND state LIKE '%$state%' AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where first_name LIKE '%$fname%' AND last_name LIKE '%$lname%' AND state LIKE '%$state%' AND active =1"),0);
$search_term = "First Name: ".$_GET['first_name']."     Last Name: ".$_GET['last_name']."     State: ".$_GET['state'];
}
else if($fname=="" AND $lname=="") 
{
$sql = mysql_query("SELECT * FROM person where state LIKE '%$state%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where state LIKE '%$state%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "City / Area / Postal Code: ".$_GET['city']."     State: ".$_GET['state'];
}
else if($fname=="" AND $state=="") 
{
$sql = mysql_query("SELECT * FROM person where last_name LIKE '%$lname%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where last_name LIKE '%$lname%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "Last Name: ".$_GET['last_name']."     City / Area / Postal Code: ".$_GET['city'];
}
else if($fname=="" AND $city=="") 
{
$sql = mysql_query("SELECT * FROM person where last_name LIKE '%$lname%' AND state  LIKE '%$state%' AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where last_name LIKE '%$lname%' AND state  LIKE '%$state%' AND active =1"),0);
$search_term = "Last Name: ".$_GET['last_name']."     State: ".$_GET['state'];
}
else if($lname=="" AND $state=="") 
{
$sql = mysql_query("SELECT * FROM person where first_name LIKE '%$fname%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where first_name LIKE '%$fname%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "First Name: ".$_GET['first_name']."     City / Area / Postal Code: ".$_GET['city'];
}
else if($lname=="" AND $city=="") 
{
$sql = mysql_query("SELECT * FROM person where first_name LIKE '%$fname%' AND state  LIKE '%$state%' AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where first_name LIKE '%$fname%' AND state  LIKE '%$state%' AND active =1"),0);
$search_term = "First Name: ".$_GET['first_name']."     State: ".$_GET['state'];
}
else if($state=="" AND $city=="") 
{
$sql = mysql_query("SELECT * FROM person where first_name LIKE '%$fname%' AND last_name  LIKE '%$lname%' AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where first_name LIKE '%$fname%' AND last_name  LIKE '%$lname%' AND active =1"),0);
$search_term = "First Name: ".$_GET['first_name']."     Last Name: ".$_GET['last_name'];
}
else if($fname=="" AND $lname=="" AND $state=="") 
{
$sql = mysql_query("SELECT * FROM person where active =1 AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where active =1 AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%')"),0);
$search_term = "City / Area / Postal Code: " .$_GET['city'];
}
else if($fname=="" AND $lname=="" AND $city=="") 
{
$sql = mysql_query("SELECT * FROM person where state  LIKE '%$state%' AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where state  LIKE '%$state%' AND active =1"),0);
$search_term = "State: " .$_GET['state'];

}
else if($fname=="" AND $state=="" AND $city=="") 
{
$sql = mysql_query("SELECT * FROM person where last_name  LIKE '%$lname%' AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where last_name  LIKE '%$lname%' AND active =1"),0);
$search_term = "Last Name: " .$_GET['last_name'];
}
else if($lname=="" AND $state=="" AND $city=="") 
{
$sql = mysql_query("SELECT * FROM person where first_name  LIKE '%$fname%' AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where first_name  LIKE '%$fname%' AND active =1"),0);
$search_term = "First Name: " .$_GET['first_name'];
}
else 
{
$sql = mysql_query("SELECT * FROM person where first_name LIKE '%$fname%' AND last_name LIKE '%$lname%' AND state LIKE '%$state%' AND (address LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person where first_name LIKE '%$fname%' AND last_name LIKE '%$lname%' AND state LIKE '%$state%' AND (address LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "Last Name: ".$_GET['last_name']."     State: ".$_GET['state']."     City / Area / Postal Code: ".$_GET['city'];
}

 

That was my PHP code.. now what it does is.. it searched according to the inputed data.. like which field is entered. As u can see it has many if condition so when i enter only the first name, it goes to some other if condition which is not matching.

 

Like I entered my first name, it went to the following code

 

else if($lname=="") 
{
$sql = mysql_query("SELECT * FROM person WHERE first_name LIKE '%$fname%' AND state LIKE '%$state%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1 ORDER BY 'hits' DESC LIMIT $from, $max_results ")
or die("Could not select data because ".mysql_error());

$total_results  = mysql_result(mysql_query("SELECT  COUNT(*) as Num FROM person WHERE first_name LIKE '%$fname%' AND state LIKE '%$state%' AND (address  LIKE '%$city%' OR city LIKE '%$city%' OR pin_code LIKE '%$city%') AND active =1"),0);
$search_term = "First Name: ".$_GET['first_name']."     State: ".$_GET['state']."     City / Area / Postal Code: ".$_GET['city'];
}

 

but it should have gone to its previous condition.. Please please help me out..

Link to comment
https://forums.phpfreaks.com/topic/166695-help-with-my-search-engine/
Share on other sites

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.