简体   繁体   English

MYSQL SELECT 查询与 WHERE 不起作用

[英]MYSQL SELECT query with WHERE not working

The data in my database has a list of names and each name has an ID.我的数据库中的数据有一个名称列表,每个名称都有一个 ID。 I am trying to make a simple search to pull the persons ID when the name matches the searched name.当名称与搜索的名称匹配时,我正在尝试进行简单的搜索以提取人员 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 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结构

If you want to retrieve the ids for names which match the input then you might want to try that with like operator, eg:如果您想检索match输入match的名称的 id,那么您可能想尝试使用like运算符,例如:

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

for case insensitive matching, try using lower function and pass the name in lower case, gg:对于不区分大小写的匹配,请尝试使用lower函数并以小写形式传递名称gg:

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

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

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