简体   繁体   中英

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.

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.

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.

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

Try to change POST to GET, see if it helps, and get back here.

In your js try this:

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

Also try to put a semicolon after you set var FormSearchData = ... ;


To use POST you d better send your FormSearchData as an object:

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

Also, use var_dump($_POST) to see what is missing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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