【问题标题】:Problems with Comparing Database Values and Text Input using MySQL and jQuery - Undefined Index and mysql_fetch_assoc() Errors使用 MySQL 和 jQuery 比较数据库值和文本输入的问题 - 未定义索引和 mysql_fetch_assoc() 错误
【发布时间】:2011-06-25 23:14:46
【问题描述】:

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

当我在 ID 为 #searchBar 的文本输入字段中输入文本时,我收到此错误:

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

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

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

代码如下:

search.js

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()));
});

searchBar.php

<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>

search.php

<?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>";
    }
?>

提前致谢!

【问题讨论】:

  • 第10行的查询返回false,不是资源,也就是说失败了,调试一下。
  • 好的,你有什么建议可以帮助调试它,因为我真的不确定问题是什么。谢谢。
  • if (!$searchResults) { die('无效查询:' .mysql_error()); }
  • 通过将“站点”更改为站点,我能够摆脱 mysql_fetch_assoc 错误,但我仍然收到通知:未定义索引:C:\wamp\www\lifesearch\requires\search 中的 searchBit .php 在第 6 行。关于如何解决此问题的任何其他建议?
  • 在分配之前检查它的集合

标签: php javascript jquery mysql post


【解决方案1】:

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

【讨论】:

  • 实际上它是在 js ajax 帖子 {searchBit:searchQuery} 中创建的
  • 是的,不幸的是,这并没有解决问题。还有其他建议吗?我仍然收到未定义的错误。
  • 我不确定这是错误还是什么,但我在收件箱中收到了一条消息,询问“你试过这个吗?”但由于某种原因我在这里看不到它,所以我不确定所指的是什么。
  • @Kevin Gurney 抱歉,我没有注意到 {searchBit:searchQuery}。尝试在您的 php 脚本中调试发布的数据,可能会转储 $_POST 的内容以检查实际到达那里的内容。
  • 好的,所以我尝试了一个 var_dump,它返回了 string(0) "" 和 NULL 这是不正确的,因为我在输入字段中输入了几个字母,所以有人知道可能是什么原因造成的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多