PHP自动完成无法正常工作

时间:2013-11-23 17:57:28

标签: php jquery mysql

我的所有html工作正常但是当我尝试自动填充字段时,我的php代码似乎有问题

search.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="css/style.css" rel="stylesheet" type="text/css">

 <SCRIPT LANGUAGE="JavaScript" src="js/jquery.js"></SCRIPT>
 <SCRIPT LANGUAGE="JavaScript" src="js/script.js"></SCRIPT>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

   <div class="main">
      <div class=""><a href="http://www.scriptime.blogspot.in">scriptime</a></span></div>
         <div id="holder"> 
         Enter Keyword : <input type="text" id="keyword" tabindex="0"><img src="images/loading.gif" id="loading">
         </div>
         <div id="ajax_response"></div>

   </div>
</body>
</html>

这里是我的PHP代码

names.php

<?php
    include("Connections/myphp.php");
    $keyword = $_POST['data'];
    $sql = "select username from ".$users." where ".$username." like '".$keyword."%' limit 0,20";
    //$sql = "select username from ".$users."";
    $result = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($result))
    {
        echo '<ul class="list">';
        while($row = mysql_fetch_array($result))
        {
            $str = strtolower($row['username']);
            $start = strpos($str,$keyword); 
            $end   = similar_text($str,$keyword); 
            $last = substr($str,$end,strlen($str));
            $first = substr($str,$start,$end);

            $final = '<span class="bold">'.$first.'</span>'.$last;

            echo '<li><a href=\'javascript:void(0);\'>'.$final.'</a></li>';
        }
        echo "</ul>";
    }
    else
        echo 0;
?>  

ajax代码

/*
 cc:scriptime.blogspot.in
 edited by :midhun.pottmmal
*/
$(document).ready(function(){
    $(document).click(function(){
        $("#ajax_response").fadeOut('slow');
    });
    $("#keyword").focus();
    var offset = $("#keyword").offset();
    var width = $("#keyword").width()-2;
    $("#ajax_response").css("left",offset.left); 
    $("#ajax_response").css("width",width);
    $("#keyword").keyup(function(event){
         //alert(event.keyCode);
         var keyword = $("#keyword").val();
         if(keyword.length)
         {
             if(event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)
             {
                 $("#loading").css("visibility","visible");
                 $.ajax({
                   type: "POST",
                   url: "names.php",
                   data: "data="+keyword,
                   success: function(msg){  
                    if(msg != 0)
                      $("#ajax_response").fadeIn("slow").html(msg);
                    else
                    {
                      $("#ajax_response").fadeIn("slow");   
                      $("#ajax_response").html('<div style="text-align:left;">No Matches Found</div>');
                    }
                    $("#loading").css("visibility","hidden");
                   }
                 });
             }
             else
             {
                switch (event.keyCode)
                {
                 case 40:
                 {
                      found = 0;
                      $("li").each(function(){
                         if($(this).attr("class") == "selected")
                            found = 1;
                      });
                      if(found == 1)
                      {
                        var sel = $("li[class='selected']");
                        sel.next().addClass("selected");
                        sel.removeClass("selected");
                      }
                      else
                        $("li:first").addClass("selected");
                     }
                 break;
                 case 38:
                 {
                      found = 0;
                      $("li").each(function(){
                         if($(this).attr("class") == "selected")
                            found = 1;
                      });
                      if(found == 1)
                      {
                        var sel = $("li[class='selected']");
                        sel.prev().addClass("selected");
                        sel.removeClass("selected");
                      }
                      else
                        $("li:last").addClass("selected");
                 }
                 break;
                 case 13:
                    $("#ajax_response").fadeOut("slow");
                    $("#keyword").val($("li[class='selected'] a").text());
                 break;
                }
             }
         }
         else
            $("#ajax_response").fadeOut("slow");
    });
    $("#ajax_response").mouseover(function(){
        $(this).find("li a:first-child").mouseover(function () {
              $(this).addClass("selected");
        });
        $(this).find("li a:first-child").mouseout(function () {
              $(this).removeClass("selected");
        });
        $(this).find("li a:first-child").click(function () {
              $("#keyword").val($(this).text());
              $("#ajax_response").fadeOut("slow");
        });
    });
});

当我尝试搜索名称时,它会给我一个错误说:你的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在第1行的'where like'ben%'limit 0,20'附近使用正确的语法

2 个答案:

答案 0 :(得分:0)

$sql = "select username from ".$users." where ".$username." like '".$keyword."%' limit 0,20";

您错过了$users$username他们的价值观是什么?

答案 1 :(得分:0)

2件事:

我认为你不需要data:"data="+keyword,查看你的php data:keyword就足够了。

其次, 尝试将您的查询更改为:

$sql = "select username from users where username like '".$keyword."%' limit 0,20";

因为你的php似乎没有设置$users$username