...

View Full Version : Problems with Multiple field search function



KurveMedia
12-15-2008, 04:46 AM
I'm not sure what im doing wrong with the following code. The purpose of the code is to take input from a drop down and 2 text fields and search the database and display the information that matches the criteria. The first criteria is the drop down which is to choose whether you want to search by the name of the business or the main category that its in. the second is where you enter the name of the business or the main category that business is in. the second text field is to enter the city if your trying to find a particular business type in a particular city.not sure what im doing wrong, but its not bring up any information or its just bringing up everything in that city in the database no mater what kind of business. below is the code that im using




$link = mysql_connect($host,$username,$password);
if (!$link)
{
echo "we could not establish a connection to the sql server! please retry later";
exit;
}
if (!mysql_select_db($db))
{
echo "we could not connect to the database! please retry later";
exit;
}

$where = "WHERE 1 ";
if($_GET['name'] != "") {
$where .= " AND ".$_GET['type']." like '%".$_GET['name']."%'";
}
if($_GET['city'] != "")
{
$where .= " AND BusinessCity like '%".$_GET['city']."%'";
}
//$sql = "SELECT * FROM listings WHERE BusinessCity = '".$_GET['city']."' AND ".$_GET['type']." = '".$_GET['name']."'";
$sql = "SELECT * FROM listings ".$where;
$namecatquery = mysql_query($sql);
if($namecatquery != true)
{
echo "error";
}
$count = mysql_num_rows($namecatquery);
?>
<?php
while($array = mysql_fetch_array($namecatquery))
{

lokeshshettyk
12-15-2008, 07:36 AM
Did you ever try to ECHO the query and paste the same on to mysql console/UI and see what you get outta it?

Do ypu always have values entered in the NAME and CITY field OR either of them?

KurveMedia
12-18-2008, 01:32 AM
lokeshshettyk

if a customer wants to find a particular kind of business in a particular city then that's when the name and would be filled in or if they wanted to find the listing for a business they already know they would enter the name only. and choose business name in the drop and not the type.

i didn't write this script so im alittle lost on this script. i know what its supposed to do, just not sure why its not working correctly. it was working a while ago. but stopped working correctly.

lokeshshettyk
12-18-2008, 09:48 AM
Ok then, You may wanna try this...
PS: try REQUEST instead of GET
I don't understand this $_GET['type'] ????


$where = "WHERE 1 "; // I do not understand this KurveMedia. Please clarify...

if(!empty($_REQUEST['name']) && !empty($_REQUEST['city'])) { // when both are entered
$where .= pass the values of name and city vars to the query
}
if(!empty($_REQUEST['name']) && empty($_REQUEST['city'])) { // when only name is entered
$where .= pass the name var to the query
}
if(empty($_REQUEST['name']) && !empty($_REQUEST['city'])) { // when only the city is entered - in case you want this option
$where .= pass the city var to the query
}
if(empty($_REQUEST['name']) && empty($_REQUEST['city'])) { // when nothing is entered - in case you want this option
$where .= error message
}
//$sql = "SELECT * FROM listings WHERE BusinessCity = '".$_REQUEST['city']."' AND ".$_REQUEST['type']." = '".$_REQUEST['name']."'";
$sql = "SELECT * FROM listings ".$where;



Hope this helps :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum