Problems in searching some words in database

0

I have a course table that has 3067 records, create a search engine to filter by courses my problem is based when I put the word "anat" I get all the options (anatomy, human anatomy, clinical anatomy, ect), but when I place calculation, algebra or management (I also put cal, calc, alg, ges) I do not get any course to select the truth I have already tried several options but none of them gives me solutions, maybe I have problems in the query

<!DOCTYPE html>
<html>
 <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
 </head>
 <body>
  <br /><br />
  <div class="container" style="width:600px;">
   <h2 align="center">Autocomplete</h2>
   <br /><br />
   <label>Search Cursos</label>
   <input type="text" name="cursos" id="cursos" class="form-control input-lg" autocomplete="off" placeholder="Ingrese nombre del curso" />
  </div>
 </body>
</html>

<script>
$(document).ready(function(){
 
 $('#cursos').typeahead({
  source: function(query, result)
  {
   $.ajax({
    url:"fetch.php",
    method:"POST",
    data:{query:query},
    dataType:"json",
    success:function(data)
    {
     result($.map(data, function(item){
      return item;
     }));
    }
   })
  }
 });
 
});
</script>

<?php

$connect = mysqli_connect("localhost", "root", "", "encuesta1");
$request = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
 SELECT * FROM cursos WHERE nombre LIKE '%".$request."%'
";

$result = mysqli_query($connect, $query);

$data = array();

if(mysqli_num_rows($result) > 0)
{
 while($row = mysqli_fetch_assoc($result))
 {
  $data[] = $row["nombre"];
 }
 echo json_encode($data);
}

?>

I attach the code if someone can help me or guide me, it would be very helpful.

    
asked by claudia24 17.10.2017 в 23:04
source

1 answer

0

After the sentence

$request = mysqli_real_escape_string($connect, $_POST["query"]);

Put the following:

$request = htmlspecialchars($request);
    
answered by 17.10.2017 в 23:41