【问题标题】:Unable to recover a value from MySQL无法从 MySQL 中恢复值
【发布时间】:2013-09-03 18:23:16
【问题描述】:

我有以下代码,与test2.php 相关。我的问题是我必须将菜单中选择的用户与其唯一 ID 链接起来,因此 URL 将是所选用户的页面。问题是 id 未定义。

我也不知道为什么!我究竟做错了什么?来自

的 URL
window.location.href = '/profile.php?id='+pieces[1];

/profile.php?id=undefined

$("#course").autocomplete("/test/test2.php", {
        selectFirst: false,
        formatItem: function(data, i, n, value) {
            //make the suggestion look nice
            return "<font color='#3399CC'>" + value.split("::")[0] + "</font>";
        },
        formatResult: function(data,value) {
            //only show the suggestions and not the URLs in the list
            return value.split("::")[0];
     var username = splitItems[0];
         var id = splitItems[1];
          return username;
        }
    }).result(function(event, data, formatted) {
         //redirect to the URL in the string
    var pieces = formatted.split("::");
        window.location.href = '/profile.php?id='+pieces[1];

test2.php

<?php
mysql_connect ("****", "****","****")  or die (mysql_error());
mysql_select_db ("****");
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select Username, id from **** where Username LIKE '%$q%'";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $cname = $rs['Username'];
    echo "$cname\n";
}
?>

【问题讨论】:

  • 尝试记录这些片段,看看它有什么console.log(pieces)

标签: php autocomplete jquery-autocomplete


【解决方案1】:

对于你的第一个代码,你应该用这个替换它:

$("#course").autocomplete("/test/test2.php", {
        delay: 0,
      selectFirst: false,
        formatItem: function(data, i, n, value) {
        //make the suggestion look nice
        var pièces = value.split("::");
              return "<font color='#000000'; font size='3pt'; font face='Arial'>" + pièces[0]  + "</font>";
        },
        formatResult: function(data,value) {
            //only show the suggestions and not the URLs in the list
        var pièces = value.split("::");

                return pièces[0];

        }
    }).result(function(event, data, formatted) {
    //redirect to the URL in the string

    var pieces = formatted.split("::");
          window.location.href = '/profile.php?id='+pieces[1];

第二个代码

你应该添加:

$id = $rs['id'];

$cname = $rs['Username'];

并将此更改添加到以下行。

echo (utf8_encode("$cname::$id\n"));

【讨论】:

    【解决方案2】:

    我不认为将 /test/test2.php 放入您的脚本中实际上包含并运行 test2.php。

    试试这个

    $("#course").autocomplete("<?php include '/test/test2.php'; ?>", {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多