简体   繁体   中英

MYSQL SELECT query with WHERE not working

The data in my database has a list of names and each name has an ID. I am trying to make a simple search to pull the persons ID when the name matches the searched name.

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

If you want to retrieve the ids for names which match the input then you might want to try that with like operator, eg:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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