Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problems with Multiple field search function

    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

    PHP Code:

    $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))
        {

  • #2
    Regular Coder lokeshshettyk's Avatar
    Join Date
    Aug 2008
    Location
    On the way to the moon!
    Posts
    157
    Thanks
    5
    Thanked 20 Times in 20 Posts
    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?

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    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.

  • #4
    Regular Coder lokeshshettyk's Avatar
    Join Date
    Aug 2008
    Location
    On the way to the moon!
    Posts
    157
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Ok then, You may wanna try this...
    PS: try REQUEST instead of GET
    I don't understand this $_GET['type'] ????
    PHP Code:
    $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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •