I'm trying to use autocomplete using php, mysql and jquery. However, I get an error in PHP code that says:
Fatal error: Call to a member function bind_param() on a non-object in C:\Users\Admin\Documents\USBWebserver v8.6\root\kk.php on line 8
This is my php code:
<?php
include 'config.php'; // this works fine
$keyword = '%'.$_POST['keyword'].'%';
$sql = "SELECT * FROM person WHERE firstName LIKE (:keyword)";
$query = $conn->prepare($sql);
$query->bind_param(':keyword', $keyword); // THIS IS LINE 8 WHERE THE ERROR IS COMING FROM
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
// put in bold the written text
$fname = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['firstName']);
// add new option
echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['firstName']).'\')">'.$fname.'</li>';
}
?>
This is the javascript file:
<script>
function auto_complete() {
var min_length = 0;
var keyword = $('#names_id').val();
if (keyword.length >= min_length) {
$.ajax({
url: 'kk.php',
type: 'POST',
data: {keyword:keyword},
success:function(data){
$('#names_list_id').show();
$('#names_list_id').html(data);
}
});
} else {
$('#names_list_id').hide();
}
}
function set_item(item) {
// change input value
$('#names_id').val(item);
// hide proposition list
$('#names_list_id').hide();
}
</script>
I want to get the first name, middle name and last name.
This wasn't taught in school and we need it for our project. I got the code from http://www.bewebdeveloper.com/tutorial-about-autocomplete-using-php-mysql-and-jquery. Can you all please help me? Thank you.