Php 使用MySQL和jQuery比较数据库值和文本输入时出现问题-未定义索引和MySQL\u fetch\u assoc()错误

Php 使用MySQL和jQuery比较数据库值和文本输入时出现问题-未定义索引和MySQL\u fetch\u assoc()错误,php,javascript,jquery,mysql,post,Php,Javascript,Jquery,Mysql,Post,我在将文本输入与本地数据库中存储的值进行比较时遇到问题 当我在id为#searchBar的文本输入字段中键入文本时,我收到以下错误: 注意:第6行的C:\wamp\www\lifesearch\requires\search.php中的未定义索引:searchBit 我已经尝试了一段时间来解决这个问题,但没有任何真正的成功。我不明白为什么它说索引是未定义的 如果有人能提供一个可能的解决方案,我将不胜感激 代码如下: search.js searchBar.php search.php 表单输

我在将文本输入与本地数据库中存储的值进行比较时遇到问题

当我在id为#searchBar的文本输入字段中键入文本时,我收到以下错误:

注意:第6行的C:\wamp\www\lifesearch\requires\search.php中的未定义索引:searchBit

我已经尝试了一段时间来解决这个问题,但没有任何真正的成功。我不明白为什么它说索引是未定义的

如果有人能提供一个可能的解决方案,我将不胜感激

代码如下:

search.js

searchBar.php


search.php


表单输入字段没有名称属性,该属性应为“searchBit”$_POST['searchBit']未定义,因为..

第10行上的查询返回false,而不是资源,换句话说,它失败了,请调试它。好的,您有什么建议来帮助调试它,因为我真的不确定问题出在哪里。感谢.if(!$searchResults){die('Invalid query:'.mysql_error());}通过将“sites”更改为sites,我能够消除mysql_fetch_assoc错误,但我仍然收到通知:未定义索引:第6行的C:\wamp\www\lifesearch\requires\search.php中的searchBit。关于如何解决这个问题,还有其他建议吗?在分配它之前,请检查它的集合。它实际上是在js ajax post{searchBit:searchQuery}中创建的。如果没有设置,搜索将失败,我想他可能会注意到,是的,不幸的是,这并没有解决问题。还有其他建议吗?我仍然收到未定义的错误。我不确定这是一个bug还是什么,但我在收件箱中收到一条消息,问“你尝试过这个吗?”但由于某种原因,我无法在这里看到它,所以我不确定引用了什么。@Kevin Gurney抱歉,我没有注意到{searchBit:searchQuery}。尝试调试php脚本中发布的数据,可能是vardump$u POST的内容,以检查是否真的到达了那里。好的,所以我尝试了一个varu dump,它返回了字符串(0)”,NULL,这是不正确的,因为我在输入字段中键入了几个字母,所以有人知道是什么导致了这个问题吗?
function search(searchQuery) {
    $.post("requires/search.php", {searchBit:searchQuery}, function(data) {
        $("#searchResults").html(data);
    });
}

$(document).ready(function() {
    $("#searchBar").focus(function() {
        if ($("#searchBar").val() == "start searching...") {
            $("#searchBar").val("");
        }
        $(this).addClass("searchBarFocus");
        $(this).removeClass("searchBarBlur");
    });
    $("#searchBar").blur(function() {
        $(this).addClass("searchBarBlur");
        $(this).removeClass("searchBarFocus");
    });
    $("#searchBar").keypress(function() {
        $("#searchResults").fadeOut(200);
        $("#searchResults").fadeIn(200);
    });
    $("#search").mouseleave(function() {
        $("#searchResults").fadeOut(200);
    });
    $("#searchBar").keyup(search(this.val()));
});
<div id="search">
    <form action="requires/search.php">
        <input type="text" value="start searching..." id="searchBar" class="searchBarBlur" 
        autocomplete="off"/>
    </form>
    <div id="searchResults">
    </div>
</div>
<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $database = "websites";
    $searchBit = $_POST['searchBit'];
    mysql_connect($host, $username, $password);
    mysql_select_db($database) or die("Failed to select table from database");
    $userSearch = mysql_real_escape_string(addslashes($searchBit));
    $searchResults = mysql_query("SELECT name FROM sites WHERE name LIKE '$userSearch%'");

    while ($row = mysql_fetch_assoc($searchResults)) {
        echo "<div class='searchResult'>".$row['name']."</div>";
    }
?>