PHP MySQL search with multiple criteria

前端 未结 3 451
温柔的废话
温柔的废话 2021-01-23 15:04

I have a search form in a website and would like to have several search terms which is input by the user to perform db search, terms as below:

  • Keywords
  • Pr
相关标签:
3条回答
  • 2021-01-23 15:17

    Instead of taking these variables you should use ".=" operator.

    /*      $t1 = '';
            $t2 = '';
            $t3 = '';
            $t4 = '';
            $t5 = '';
    */
            $q = "SELECT * FROM `property` WHERE `status`='1' ";
    
    // You need to enclose all **OR** logical tests in parenthesis.
    // Moreover most of the usages of isset function are useless,
    // as your are initializing many variables
    
            if($keyword && !empty($keyword)){
                $q .= " AND (`p_title` LIKE '%".$keyword."%' OR `address` LIKE '%".$keyword."%' OR `area` LIKE '%".$keyword."%')";
            }
            if($prop_for && !empty($prop_for)){
    // If you are using double quotes you really don't need  handle to concatenation.
            $q .= " AND `p_for`='$prop_for'";
            }
            if($state && !empty($state)){
                $q .= " AND `state`='$state'";
            }
            if($ptype && !empty($ptype)){
                $q .= " AND `p_category`='$ptype'";
            }
            //min only
            if($min_price && !empty($min_price)){
                $q .= " AND `price` >= '".$min_price."'";
            }
            //max only
            if($max_price && !empty($max_price)){
                $q .= " AND `price` <= '$max_price'";
            }
    
    // When you are not using OFFSET keyword,
    //the first number after LIMIT keyword should be the number of records
                    $q .= " ORDER BY `posted_date` DESC LIMIT $per_page , $start;";
                    $sql = $mysqli->query($q);
    
    0 讨论(0)
  • 2021-01-23 15:17
    if(isset($_SESSION['login']))
    {
     echo "<div align=\"right\"><strong><a href=\"index.php\"> Home </a>|
     <a href=\"signout.php\">Signout</a>|
     <a href=\"home.php\">Profile</a></strong></div>";
    
    
     }
     else
     {
        echo "&nbsp;";
     }
    
    $con=  mysql_connect("localhost","root","");
      $d=mysql_select_db("matrimonial",$con);
       $gender=$_POST['gender'];
      $age1=$_POST['age1'];
      $age2=$_POST['age2'];
      $city=$_POST['city'];
      $subcast=$_POST['subcast'];
      $result=mysql_query("select * from matri where gender='$gender' and age between '$age1' and '$age2' and city='$city' and subcast='$subcast'");
    
    if($gender && !empty($gender))
    {
     $result .= " AND `gender`='$gender'";
    }
    
    if($age1 && !empty($age1)){
            $result .= " AND `age`='$age1'";
        }   
     if($age2 && !empty($age2)){
           $result .= " AND `age`='$age2'";
        }  
    
     if($city && !empty($city)){
           $result .= " AND `city`='$city'";
        }  
    
      if($subcast && !empty($subcast)){
           $result .= " AND `subcast`='$subcast'";
        }  
    
       $result .= " select * from ";
    
       $sql = $mysql->query($result);
    how to run this code
    
    0 讨论(0)
  • 2021-01-23 15:26

    You're going to need parentheses.

    SELECT * FROM `project` WHERE (`proj_title` LIKE '%keywords%' OR `proj_addr` LIKE '%keywords%' OR `proj_area` LIKE '%keywords%') AND `proj_for`='Sale' AND `state`='Somewhere' AND `proj_cat`='8' AND `price` BETWEEN '250000' AND '600000'
    

    Without the parentheses it just has to match one of the criteria before the last OR.

    0 讨论(0)
提交回复
热议问题