автозаполнение начальной загрузки php mysql - PullRequest
0 голосов
/ 08 мая 2019
                autocomplete bootstrap php mysql ajax 

автозаполнение начальной загрузки php mysql из базы данных, данные не извлекаются из базы данных, не отображаются подсказки мой код не показывает никаких результатов, пожалуйста, помогите завершить мой код, который я много раз менял мой код

          <!DOCTYPE html>
        <html>
     <head>
             <title>Webslesson Tutorial | Autocomplete Textbox using Bootstrap Typehead with Ajax PHP</title>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/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.7/css/bootstrap.min.css" />  
         </head>
         <body>

Автозаполнение текстового поля с помощью Bootstrap Typeahead с Ajax PHPСтрана поиска // скрипт $ (документ) .ready (function () {$ ('# страна'). Typeahead ({источник: функция (запрос, результат) {$ .ajax ({url: "autoselect_jquery5.php", метод: "POST", data: {query: query}, dataType: "json", success: function (data) {result ($. Map (данные, функция (элемент) {возвращаемый элемент;}));}})}});

        });
         </script>






         // autoselect_jquery5.php


          <?php
            include 'database.php';

             if (isset($_POST['query'])) {
            // $search_query = $_POST['query'];
              $search_query = mysqli_real_escape_string( $_POST["query"]);

          $query = "SELECT * FROM transporter WHERE address LIKE '%".$search_query."%' LIMIT 12";
     // $query = "SELECT * FROM transporter WHERE address LIKE  %' 
         $search_query ' LIMIT 12";
          $result = mysqli_query($link, $query);
        $data = array();

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

1 Ответ

0 голосов
/ 08 мая 2019

В вашем файле несколько ошибок.Я прокомментировал это полностью, чтобы помочь другим, кто сталкивался с этим вопросом.

<?php
//Assume this line is correct and that you have a database.php file containing your log in credientials
include 'database.php';

//If Statement says - run this next piece of code if $_POST['query'] is set to something 
if (isset($_POST['query']))
{

// $search_query = $_POST['query']; - Commented OUT
//This line attempts to sanatise the input from the posted data
$search_query = mysqli_real_escape_string( $_POST["query"]);

//This line constructs the whole SQL statement ( BAd methodology here, but thats a different topic)
$query = "SELECT * FROM transporter WHERE address LIKE '%".$search_query."%' LIMIT 12";

//You've commented out the next line and its of no use
// $query = "SELECT * FROM transporter WHERE address LIKE  %' 

//This line has a syntax error - but is also of no use - Should delete but should read  $search_query = ' LIMIT 12';
//$search_query ' LIMIT 12";

/// This line queries the database
$result = mysqli_query($link, $query);

//This line declares $data will be an array
$data = array();

//If the DB returns some rows
if(mysqli_num_rows($result) > 0)
{
    // While there are results
    while($row = mysqli_fetch_assoc($result))
    {
        //add to the $data array
        $data[] = $row["address"];
    }
    //Output $data in JSON format to be interpreted as a response from your ajax call
    echo json_encode($data);
 }
 }
 ?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...