PHP, AJAX, MYSQL - Przeszukiwanie bazy
Ostatnio zmodyfikowano 2021-02-16 12:50
Capitan Temat założony przez niniejszego użytkownika |
PHP, AJAX, MYSQL - Przeszukiwanie bazy » 2021-02-12 15:32:54 Witam, mam pewnie problem z kodem. Chciałbym, żeby ktoś pomógł mi znaleźć błąd. < !DOCTYPE html > < html > < head > < script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" > < / script > < script type = "text/javascript" src = "js/form.js" > < / script > < link rel = "stylesheet" type = "text/css" href = "css/css.css" media = "screen" / >
< script >
function showState( str ) { if( str == "" ) { document.getElementById( "txtHint" ).innerHTML = ""; return; } else { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if( this.readyState == 4 && this.status == 200 ) { document.getElementById( "txtHint" ).innerHTML = this.responseText; } }; xmlhttp.open( "GET", "php/get.php?q=" + str, true ); xmlhttp.send(); } }
$( document ).ready( function() { load_data(); function load_data( query ) { $.ajax( { url: "php/fetch.php", method: "POST", data: { query: query }, success: function( data ) { $( '#result' ).html( data ); } } ); } $( '#search_text' ).keyup( function() { var search = $( this ).val(); if( search != '' ) { load_data( search ); } else { load_data(); } } ); } );
< / script >
< / head > < body >
< ? php include_once 'php/dbConfig.php';
$ query = "SELECT * FROM baza_woj ORDER BY WOJ_NAME ASC"; $ result = $ db->query( $ query ); ? >
< form > < select id = "country" onchange = "showState(this.value)" > < option value = "" > < / option > < ? php if( $ result->num_rows > 0 ) { while( $ row = $ result->fetch_assoc() ) { echo '<option value="'.$ row[ 'WOJ_ID' ].'">'.$ row[ 'WOJ_NAME' ].'</option>'; } } else { echo '<option value="">BRAK</option>'; } ? > < / select >
< / form > < br > < div id = "txtHint" > < b > ... < / b > < / div >
< div class = "container" > < br / > < h2 align = "center" > Ajax Live Data Search using Jquery PHP MySql < / h2 > < br / > < div class = "form-group" > < div class = "input-group" > < span class = "input-group-addon" > Search < / span > < input type = "text" name = "search_text" id = "search_text" placeholder = "Search by Customer Details" class = "form-control" / > < / div > < / div > < br / > < div id = "result" > < / div > < / div >
< / body > < / html >
< ? php $ db = mysqli_connect( "localhost", "root", "", "baza" );
$ output = '';
if( isset( $ _POST[ "query" ] ) ) { $ search = mysqli_real_escape_string( $ db, $ _POST[ "query" ] ); echo "A: ".$ search."<br>"; $ query = "SELECT * FROM terc WHERE NAZWA '%".$ search."%'"; echo "istnieje ".$ query; } else { $ query = "SELECT * FROM terc ORDER BY WOJ"; echo "nie istnieje"; }
$ result = mysqli_query( $ db, $ query );
echo "Returned rows are: ".mysqli_num_rows( $ result );
if( mysqli_num_rows( $ result ) > 0 ) { $ output.= ' <div class="table-responsive"> <table class="table table bordered"> <tr> <th>Customer Name</th> <th>Address</th> <th>City</th> <th>Postal Code</th> <th>Country</th> </tr> '; while( $ row = mysqli_fetch_array( $ result ) ) { $ output.= ' <tr> <td>'.$ row[ "WOJ" ].'</td> <td>'.$ row[ "POW" ].'</td> <td>'.$ row[ "GM" ].'</td> <td>'.$ row[ "NAZWA" ].'</td> <td>'.$ row[ "NAZWA_DOD" ].'</td> </tr> '; } echo $ output; } else { echo 'Data Not Found'; }
? >
Podczas wyszukiwania wyskakuje błąd: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in H:\xampp\htdocs\projekt2\php\fetch.php on line 31 Returned rows are: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in H:\xampp\htdocs\projekt2\php\fetch.php on line 33 Data Not Found baza: https://i.imgur.com/LP7O5Ju.png |
|
pekfos |
» 2021-02-12 17:21:00 https://www.w3schools.com/php/func_mysqli_query.aspFor successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failure Więc wystąpił błąd, którego nie obsługujesz. W zapytaniu nie masz LIKE. |
|
Capitan Temat założony przez niniejszego użytkownika |
» 2021-02-16 12:50:31 Dobra to ogarnąłem. Zmieniłem lekko kod, żeby wyskakiwały "podpowiedzi" podczas pisania. $ connect = mysqli_connect( "localhost", "root", "", "baza" );
$ request = mysqli_real_escape_string( $ connect, $ _POST[ "query" ] );
$ query = "SELECT * FROM SIMC WHERE NAZWA LIKE '".$ request."%'";
$ result = mysqli_query( $ connect, $ query );
$ data = array();
if( mysqli_num_rows( $ result ) > 0 ) { while( $ row = mysqli_fetch_assoc( $ result ) ) { $ data[ ] = $ row[ "NAZWA" ]; } echo json_encode( $ data ); }
$( document ).ready( function() { $( '#country' ).typeahead( { source: function( query, result ) { $.ajax( { url: "php/fetch.php", method: "POST", data: { query: query }, dataType: "json", success: function( data ) { result( $.map( data, function( item ) { return item; } ) ); } } ) } } ); } );
< form method = "post" > < label > Search < / label > < input type = "text" name = "country" id = "country" class = "form-control input-lg" autocomplete = "off" placeholder = "Type Country Name" / > < input type = "submit" name = "submit" > < / form >
Podczas wpisywania pojawiają się podpowiedzi z bazy - nazwy miejscowości. Jak zrobić, by wyświetlały się 2 wartości koło siebie np. Warszawa (woj. Mazowieckie). |
|
« 1 » |