No results using jQuery autocomplete and MySQL

1

The autocomplete of this code should show me the articles I have in a database of MySql , but every time something is written inside the text box it shows me no search result . I already verified that the connection data are correct, and despite different querys I still mark the same.

<?php
$mysqli = new mysqli("localhost", "root", "root", "inventarios");
if($mysqli === false) {
    die("ERROR: No se estableció la conexión. " . mysqli_connect_error());
}

if(!isset ($_REQUEST['buscar']))
    exit;

$sql    = "select 'Nom_Art' from 'articulos' WHERE 'Nom_Art' LIKE '%$buscar%'";
$result = $conex->listaObjetos($sql);

if(count($result) < 0)
    exit;

$data = array();
foreach($result as $r) {
    $data[] = array(
        'label' => $r->buscar,
        'value' => $r->buscar
    );
}

echo json_encode($data);
flush();
?>

And that code is used here.

<!DOCTYPE html>
<html lang="es">
<head>
<title>prueba</title>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href="http://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/validationEngine.jquery.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/jquery.validationEngine.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/languages/jquery.validationEngine-es.js"></script>
<style>
    .top-buffer { 
        margin-top:20px; 
    }
</style>
</head>
   <script type="text/javascript">
   jQuery(document).ready(function(){
   $("#buscar").autocomplete({source:"llenado.php", minLength:'4'});
  });
</script>


 <body>

 <form>
<p><label for="buscar">Priueba</label>
   <input type="text" name="buscar" id="buscar"  method="get" size="35" minlength="4"/>
</p>
</form>

    

    
asked by Jorge Ramon May Garcia 08.06.2016 в 05:05
source

2 answers

1

Try it this way.

<?php
$mysqli = new mysqli("localhost", "root", "root", "inventarios");
if ($mysqli === false){
die("ERROR: No se estableció la conexión. ". mysqli_connect_error());
}
if (!isset($_REQUEST['buscar'])) exit;
$sql = "SELECT Nom_Art FROM articulos WHERE Nom_Art LIKE'%".$buscar."%'";
$result = $conex->listaObjetos($sql);
if (count($result) < 0) exit;
$data = array();
foreach ($result as $r) {
$data[] = array (
'label' => $r[0]->buscar, 
'value' => $r[1]->buscar);
}
echo json_encode($data);
flush();
?>
    
answered by 08.06.2016 в 05:49
0

The problem is that you are invoking an undeclared variable in your query.

if(!isset ($_REQUEST['buscar']))
    exit;

$sql    = "select 'Nom_Art' from 'articulos' WHERE 'Nom_Art' LIKE '%$buscar%'";

The variable $ search has no data. You need to assign the value of $ _ REQUEST ['search']

And another, for security you need to validate the data that you are going to pass to a query, to avoid sql injections.

if(!isset ($_REQUEST['buscar']))
    exit;
$buscar  = $_REQUEST['buscar'];
$buscar = mysqli_real_escape_string($buscar);
$sql    = "select 'Nom_Art' from 'articulos' WHERE 'Nom_Art' LIKE '%$buscar%'";
    
answered by 11.10.2016 в 19:27