使用WHERE的MYSQL SELECT查询无法正常工作

时间:2017-01-26 20:04:13

标签: php mysql database where

我的数据库中的数据有一个名称列表,每个名称都有一个ID。我想尝试进行简单的搜索,以便在名称与搜索到的名称匹配时拉出人员ID。

if(isset($_POST['submit'])&& empty($_POST['personName'])==FALSE ){
    $name=$_POST['personName'];

    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        echo "No numeric characters allowed";   
    }else{
        include('connect.php');
        if($con==false){
        }else if ($con==true){
            $cleanName=mysqli_real_escape_string($con,$name);
            $getPersonID="SELECT person_ID FROM person WHERE Name='$cleanName'";
            $resultGetPersonID=mysqli_query($con,$getPersonID)or die(mysqli_error($con));
            if(mysqli_num_rows($resultGetPersonID)>0){
                echo "query sucessrful";
            }else{
                echo "query failed";
            }

        }
    }

}else{
    echo "Data entered wrong";
}

HTML

     <div class="searchBox">
    <form action="getPerson.php" method="POST">
        Name: <input id='personName' type="text" name="personName" >&nbsp;<input type="submit" value="Search" id="search" name="submit">

   </form>  
  </div>

Person Table

Structure

1 个答案:

答案 0 :(得分:0)

如果要检索输入match的名称的ID,则可能需要尝试使用like运算符,例如:

SELECT person_ID FROM person WHERE Name like %<input>%

对于不区分大小写的匹配,请尝试使用lower函数并以小写形式传递名称,g.g:

SELECT person_ID FROM person WHERE lower(Name) like %<input>%