0

我已经查看了所有先前的示例,但仍然没有骰子,并且我有一个基本的 php 问题。

例子在这里。我希望能够单击一个表并让选项显示如下:

例子

在此代码中显式声明表名有效:

if($_GET['action'] == 'getOptions'){
        $category = $_GET['category'];
        $query = "SELECT `COLUMN_NAME` 
                    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
                    WHERE `TABLE_SCHEMA`='headfirstjson' AND 
                    **`TABLE_NAME`='AmericanOilProduction'**";

        $result = db_connection($query);
        //echo $result;
        $Options = array();

        while ($row = mysql_fetch_array($result)) {                
                $Options[] = $row;
        }
        echo json_encode(array("Options" => $Options));
        exit;
}

这种通过 AJAX 传入变量的组合不会:

阿贾克斯:

function getOptions(category){
var category = category.value
$.ajax({
    url: "getData.php?action=getOptions",
    type: "GET",
    dataType:"json",
    data: {category:category},
    success: function(json){
        $.each(json.Options,function(){
        var option = "<option>"+this.COLUMN_NAME+"</option>"
        $('#options').append(option)
    });  
    }
});
}

PHP:

if($_GET['action'] == 'getOptions'){
        **$category = $_GET['category']**;
        $query = "SELECT `COLUMN_NAME` 
                    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
                    WHERE `TABLE_SCHEMA`='headfirstjson' AND 
                    `TABLE_NAME`='**.$category.**'";

        $result = db_connection($query);
        //echo $result;
        $Options = array();

        while ($row = mysql_fetch_array($result)) {                
                $Options[] = $row;
        }
        echo json_encode(array("Options" => $Options));
        exit;
    }

如果有人可以帮助我,那就太好了!谢谢。

4

2 回答 2

3

您正在尝试$category与它周围的句点连接。双引号将扩展使表名称为“.AmericanOilProduction.”的变量。将代码更改为如下所示,删除连接运算符。这里不需要它们。你也应该清理你的输入......

if($_GET['action'] == 'getOptions'){
    $category = $_GET['category'];
    $query = "SELECT `COLUMN_NAME` 
                FROM `INFORMATION_SCHEMA`.`COLUMNS` 
                WHERE `TABLE_SCHEMA`='headfirstjson' AND 
                `TABLE_NAME`='$category'";

    $result = db_connection($query);
    //echo $result;
    $Options = array();

    while ($row = mysql_fetch_array($result)) {                
            $Options[] = $row;
    }
    echo json_encode(array("Options" => $Options));
    exit;
}
于 2012-05-10T17:59:00.603 回答
1

认为data:jQuery 对 URL 本身和选项中 URL 参数的存在感到困惑。

尝试将您的action参数放入data:选项中:

data: {
   category: category,
   action: 'getOptions'
},
于 2012-05-10T17:57:03.567 回答