donglian2106 2014-05-10 08:30
浏览 18
已采纳

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

i tried to use the jquery autocomplete feature uisng php,mysql,but the problem is that the sql query get all records in the table and not the only rows that match the searched word

what's the problem in this query,it get all records and not the specified one

php code:

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:

<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条回答 默认 最新

  • donglu7286 2014-05-10 08:40
    关注

    You're using the wrong $_POST variable.

    It should be:

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

    not:

    $q_title=$db->real_escape_string($_POST['asktitle']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图