简体   繁体   English

PHP 多维数组键值

[英]PHP Multidimensional Array Key Values

I am trying to build a query builder for my Project, where I can pass any array, do the SELECT statement, return results and format them back in JavaScript.我正在尝试为我的项目构建一个查询构建器,我可以在其中传递任何数组、执行 SELECT 语句、返回结果并将它们格式化回 JavaScript。

I have been trying for a few days to get this to work, but I am somewhat a novice in PHP and quite frankly the manuals dont seem to be helping.我已经尝试了几天来让它工作,但我在 PHP 方面有点新手,坦率地说,手册似乎没有帮助。

First I build the following Array:首先,我构建以下数组:

for(var i = 0; i < FormSelect.length; i++){
    if(FormSelect[i].selectedIndex != 0){
        if(WhereClause.length == 0){
            var WhereObject = {
                ColumnName: FormSelect[i].id,
                ColumnValue: FormSelect[i].value
            }
        }
        else{
            var WhereObject = {
                Operator: " AND ",
                ColumnName: FormSelect[i].id,
                ColumnValue: FormSelect[i].value
            }
        }
    }
    WhereClause.push(WhereObject);
}

I then send the request to the server as follows, which all works fine:然后我将请求发送到服务器,如下所示,一切正常:

var FormSearchData = 
    "ColumnNames=" + ColumnNames
    + "&"
    + "ViewName=" + ViewName
    + "&"
    + "WhereClause=" + WhereClause
    + "&"
    + "FormSearchExecuted=1"

var FormSearchXMLHTTPRequest = new XMLHttpRequest();
FormSearchXMLHTTPRequest.onload = function(){
    console.log(FormSearchXMLHTTPRequest.responseText);
}
FormSearchXMLHTTPRequest.open("POST", RootData + "global/search/search.php", false);
FormSearchXMLHTTPRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
FormSearchXMLHTTPRequest.send(FormSearchData);

Server Side in PHP is where problems begin. PHP 中的服务器端是问题开始的地方。

if(isset($_POST["FormSearchExecuted"]) && !empty($_POST["FormSearchExecuted"])){

if(empty($_POST["ColumnNames"])){
    $column_names = "*";
}
else{
    $column_names = $_POST["ColumnNames"];
}
if(!empty($_POST["WhereClause"])){

    if(count($_POST['WhereClause']) == 1){
        $form_search_sql = "DECLARE @ColumnNames VARCHAR SET @ColumnNames = ? DECLARE @ViewName VARCHAR(255) SET @ViewName = ? DECLARE @WhereColumn VARCHAR SET @WhereColumn = ? DECLARE @WhereValue VARCHAR SET @WhereValue = ? EXEC('SELECT ' + @ColumnNames + ' FROM ' + @ViewName + ' WHERE ' + @WhereColumn + ' = ' + @WhereValue)";
    }

    foreach($_POST['WhereClause'] as $where){
        echo $where['ColumnName'];
    }

    $form_search_parameters = array($column_names, array(&$_POST["ViewName"]), array(&$_POST["WhereClause"]));
}

I am struggling to return the ColumnName, ColumnValue and Operator Keys in PHP, as i want to pass them as variables into the parameterised query.我正在努力在 PHP 中返回 ColumnName、ColumnValue 和 Operator Key,因为我想将它们作为变量传递到参数化查询中。 Any help would be greatly appreciated, to get me on the right track.任何帮助将不胜感激,让我走上正轨。

Your whole way is leading you to use GET method, but you use POST.你的整个方式是引导你使用 GET 方法,但你使用 POST。

Try to change POST to GET, see if it helps, and get back here.尝试将 POST 更改为 GET,看看它是否有帮助,然后回到这里。

In your js try this:在你的 js 中试试这个:

FormSearchXMLHTTPRequest.open("GET", RootData + "global/search/search.php&" + FormSearchData , false);
FormSearchXMLHTTPRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
FormSearchXMLHTTPRequest.send();

And in php surely change all $_POST to $_GET并且在 php 中肯定将所有$_POST更改为$_GET

Also try to put a semicolon after you set var FormSearchData = ... ;在设置var FormSearchData = ... ;后,也尝试放置一个分号var FormSearchData = ... ;


To use POST you d better send your FormSearchData as an object:要使用 POST,您最好将 FormSearchData 作为对象发送:

var FormSearchData = {
  "ColumnNames": ColumnNames,
  "ViewName": ViewName,
  "WhereClause": WhereClause,
  "FormSearchExecuted": 1
};

Also, use var_dump($_POST) to see what is missing另外,使用var_dump($_POST)查看缺少的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM