自动填充功能无法获得单词的精确搜索查询

时间:2014-05-10 08:30:02

标签: php jquery mysql autocomplete

我试图使用jquery自动完成功能uisng php,mysql,但问题是sql查询获取表中的所有记录而不是匹配搜索词的唯一行

此查询中的问题是什么,它获取所有记录而不是指定的记录

php代码:

if(isset($_POST['search_word']) && $_POST['search_word'] != ''){
    $q_title=$db->real_escape_string($_POST['asktitle']);


    $search_q=$db->query("select * from stack_ask where q_title like '%$q_title%' order by id desc limit 5 ");
    echo $search_q->num_rows;
    while($row=$search_q->fetch_assoc()){
        $row_title=$row['q_title'];

        ?>
        <div class="display_box" align="right">
        <a href="question?id=#" target="_blank"><?php echo $row_title; ?></a>
        </div>

        <?php

        }   

        }else{

            }

JS

//autosuggestion
$('#asktitle').keyup(function(){
    var search_word = $(this).val();
    if(search_word.length > 4){
    $('#search_box').css({'display':'block'});
    $.ajax({
    type: 'POST',
    data: 'search_word='+search_word,
    url: 'includes/process_autosuggest.php',
    beforeSend: function(){
    $('#search_load').css({'display':'block'});
    },
    success: function(data){
    $('#search_load').css({'display':'none'});
    if(data == 1){
        $('#result_s').text('Searching 0 results .');
    }else {
        $('#result_s').html(data);
    }
    }


    });
    }else {
    $('#search_box').fadeOut('fast');
    }
});
$('#asktitle').blur(function(){
    $('#search_box').fadeOut('fast');
});

形式:

<form method="POST" action="" id="postproject" >
<input dir="rtl" id="asktitle" name="asktitle" type="text" autocomplete="off"  placeholder="ضع عنوان مناسب لسؤالك" class="validate[required,maxSize[50]] text-input"  required> 
</form>

1 个答案:

答案 0 :(得分:1)

您使用了错误的$_POST变量。

应该是:

$q_title=$db->real_escape_string($_POST['search_word']);

$q_title=$db->real_escape_string($_POST['asktitle']);