简体   繁体   English

MySQL多重条件

[英]MySQL Multiple Conditions

I am trying to search my database, I initially had it working with only one and condition but now with four, it will just refresh the page, removing field inputs. 我试图搜索我的数据库,最初我只使用一个条件,而现在使用四个条件,它将刷新页面,删除字段输入。

I have tried using only a comma's but this appears to also not work. 我尝试仅使用逗号,但这似乎也不起作用。 I have been unable to find any information on Google about their being a limit but also no examples using four conditions. 我一直无法在Google上找到有关限制的任何信息,但是也没有使用四个条件的示例。 Is there a correct or better way I could try? 有没有正确或更好的方法可以尝试?

if(isset($_POST['search'])){
   $lname = trim($_POST['lname']);
   $address = trim($_POST['address']);
   $street = trim($_POST['street']);
   $city = trim($_POST['city']);
   $county = trim($_POST['county']);

   if($lname==""){
      $error[] = "Provide Last Name."; 
   }elseif($address=""){
       $error[] = "Provide House Number/Name.";
   }elseif($street==""){
      $error[] = "Provide Street.";
   }elseif($city==""){
       $error[] = "Provide City.";
   }elseif($county==""){
       $error[] = "Provide County.";
   }else{
       try{
           $stmt = $conn->prepare("SELECT * FROM clients WHERE lname=lname AND address=address AND street=street AND city=city AND county=county");
           $stmt->execute(array("lname"=>$lname, "address"=>$address, "street"=>$street, "city"=>$city, "county"=>$county));
           $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

           if($userRow==''){
               $user->redirect('./results.php?new='.$lname.$street);
           }else{
               if($userRow['rating'] == 'good'){
                   $user->redirect('./results.php?good='.$lname.$street);
               }
               if($userRow['rating'] == 'cautionextra'){
                   $user->redirect('./results.php?cautionextra='.$lname.$street);
               }
                if($userRow['rating'] == 'cautiondiscount'){
                   $user->redirect('./results.php?cautiondiscount='.$lname.$street);
               }
               if($userRow['rating'] == 'cautionlate'){
                   $user->redirect('./results.php?cautionlate='.$lname.$street);
               }
               if($userRow['rating'] == 'badnopay'){
                   $user->redirect('./results.php?badnopay='.$lname.$street);
               }
               if($userRow['rating'] == 'badabusive'){
                   $user->redirect('./results.php?badabusive='.$lname.$street);
               }
           }
        }
        catch(PDOException $e){
             echo $e->getMessage();
        }
    } 
}

Executing SELECT * FROM clients WHERE lname=lname AND address=address AND street=street AND city=city AND county=county in phpmyadmin returns all of the rows, great but when I var_dump($userRow); 在phpmyadmin中SELECT * FROM clients WHERE lname=lname AND address=address AND street=street AND city=city AND county=county执行SELECT * FROM clients WHERE lname=lname AND address=address AND street=street AND city=city AND county=county返回所有行,但是很棒,但是当我var_dump($userRow); , it shows the results for a row different to the one of which I tried to search for. ,它显示的行结果与我尝试搜索的结果不同。

You don't need: ":lname" in execute(array(...)) just use index without colon in front: "lname" . 您不需要: execute(array(...)) ":lname"只需在前面使用不带冒号的索引: "lname"

Full line: 全线:

$stmt->execute(array("lname"=>$lname, "address"=>$address, "street"=>$street, "city"=>$city, "county"=>$county));

The issue is that using an equality test for the search columns makes it difficult to get a match with any row because: 问题在于,对search列使用equality test会使与任何行的匹配变得困难,因为:

  • the entered values must match exactly to the column in the table row. 输入的值必须与表格行中的列完全匹配。
  • all the search columns must be entered. 必须输入所有搜索列。 If they are empty then they will never match any row. 如果它们为空,则它们将永远不会匹配任何行。

The approach with search fields is: 使用搜索字段的方法是:

  • try to approximately match the input value with column value by using like instead of = as the comparison operator. 尝试通过使用like而不是=作为比较运算符来使input valuecolumn value approximately match
  • make an empty input value match any column value . 使empty input value任何列值匹配。 ie ignore the test 即忽略测试

The supplied code will generate code: column like % for empty input or will wrap the input value in wildcards: column like %input value% 提供的代码将生成代码: column like %表示空输入,或将输入值包装在通配符中: column like %input value%

Untested source at Pastebin.com Pastebin.com上未经测试的来源

Untested code: 未经测试的代码:

<?php // completely untested code...
    $stmt = $conn->prepare("SELECT *
                           FROM clients
                           WHERE  lname   like :lname
                            AND   address like :address
                            AND   street  like :street
                            AND   city    like :city
                            AND   county  like :county");

    $stmt->execute(array("lname"  => empty($lname) ? '%' : '%'. $lname .'%',
                         "address" => empty($address) ? '%' : '%'. $address .'%',
                         "street"  => empty($street) ? '%' : '%'. $street .'%',
                         "city"    => empty($city) ? '%' : '%'. $city .'%',
                         "county"  => empty($county) ? '%' : '%'. $county .'%'));

    $userRow = $stmt->fetch(PDO::FETCH_ASSOC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM