PHP-SQl自动完成搜索

时间:2012-06-14 19:09:09

标签: php sql search

我正在编写一个代码来登录用户并显示他的各种细节。这工作得非常好。除此之外;我添加了一个似乎不起作用的自动完成搜索。我正在使用Jquery的自动完成功能。搜索框中的字段将从SQL中搜索。

但没有任何反应。我在文本字段中键入文本但没有任何反应。

这是我整个更正的PHP代码(不包括连接文件)

<script>
$(document).ready(function() {
  $("input#autocomplete").autocomplete({
    source: keywordList,
    minLength: 1,

  });
});
</script>

<?php echo keywordArray(); ?>
<?php function keywordArray()
{
  $rsKeywords = mysql_query("SELECT * FROM job");

  $output = '<script>'."\n";
  $output .= 'var keywordList = [';

  while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
  {
    $output .= '"'.$row_rsKeywords['work'].'",';
  }

  $output = substr($output,0,-1); //Get rid of the trailing comma
  $output .= '];'."\n";
  $output .= '</script>';
  return $output;
}
?> 

//搜索脚本:

<?php
if(!isset($_POST['submit'])){
                        echo "Your search was invalid";
                        exit;
                    } 

                    $keyword = mysql_real_escape_string($_POST['keywords']);
                    $sql = "SELECT * FROM job WHERE work='$keyword' or work LIKE 'ANOTHER_PARAMETER' LIMIT 5";

                    $result = mysql_query($sql);
                    $numrows = mysql_num_rows($result);

                    echo //details etc
>?

1 个答案:

答案 0 :(得分:2)

这是我整个更正的PHP代码(不包括连接文件)

<script>
$(document).ready(function() {
  $("input#autocomplete").autocomplete({
    source: keywordList,
    minLength: 1,

  });
});
</script>

<?php echo keywordArray(); ?>
<?php function keywordArray()
{
  $rsKeywords = mysql_query("SELECT * FROM job");

  $output = '<script>'."\n";
  $output .= 'var keywordList = [';

  while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
  {
    $output .= '"'.$row_rsKeywords['work'].'",';
  }

  $output = substr($output,0,-1); //Get rid of the trailing comma
  $output .= '];'."\n";
  $output .= '</script>';
  return $output;
}
?> 

//搜索脚本:

<?php
if(!isset($_POST['submit'])){
                        echo "Your search was invalid";
                        exit;
                    } 

                    $keyword = mysql_real_escape_string($_POST['keywords']);
                    $sql = "SELECT * FROM job WHERE work='$keyword' or work LIKE 'ANOTHER_PARAMETER' LIMIT 5";

                    $result = mysql_query($sql);
                    $numrows = mysql_num_rows($result);

                    echo //details etc
>?