Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:13:12+00:00 2026-06-09T21:13:12+00:00

I am trying to build a robust, simple, safe, catch-all way to pass dynamic

  • 0

I am trying to build a robust, simple, safe, catch-all way to pass dynamic SQL queries via ajax to a php script. I don’t think I have come up with the winner yet, but what I have now seems to work for my applications. I wish to have just one php script on my web server that take input variables to build an SQL query, execute the query, and return the results to the website. Executing the query and returning the results are not the problem. The problem is the proper and secure way to pass these input variables to the script.

The biggest confusion I am having is how to produce produce the WHERE clause still being able to account for all the different WHERE clauses that are possible.

Let’s say that the PHP file on the server is called master.php.

From Javascirpt I have this:

$.ajax({
  type: "POST",
  url: "master.php",
  dataType: "xml",
  data: { 
      schema: "the_schema", 
      table: "the_table",
      select: JSON.stringify(['col_name_1','col_name_2','...']),
      where: JSON.stringify(["status","!=","Some Status","and","STR_TO_DATE(date,","%m/%d/%Y",")",">","(date(now())","-","INTERVAL","30","day)"])
  },
  success: function (data){
      alert(data);
      // of course here I will actually do something with the data, this is just for illustration of how the data is returned to the web page.
  }
});

The data variable will either be XML data from the database, of a string: “failure: error msg”.

In the master.php script on the server I have this:

<?php
    include '/config_file.php'; //checks user permissions and establishes mysqli connection

    /*
     * Table and schema are required 
     */
    $table = $mysqli->real_escape_string($_POST['table']);
    $schema = $mysqli->real_escape_string($_POST['schema']);

    /*
     * Select section
     * if there is no select then just use *
     */
    if (isset($_POST['select'])){
        $select_string = 'select ';
        $select = json_decode($_POST['select']);
        foreach($select as $key => $value){
            $select_string .= '`'.$mysqli->real_escape_string($value).'`, ';
        }
    }else{
        $select_string = 'select * ';
    }

    /*
     * Where section
     */
    if (isset($_POST['where'])){
        $where = json_decode($_POST['where']);
    }

    if (isset($where)){
        $where_string = ' where ';
        foreach ($where as $key => $value){
            if (strpos($value, ' ') !== false or strpos($value, '%') !== false){
                // if there is a space or % in the value, it must be a string so enclose it in quotes
                $where_string .= '"'.$mysqli->real_escape_string($value).'" ';
            }else{
                $where_string .= $mysqli->real_escape_string($value).' ';
            }
        }
        $where_string = rtrim($where_string);
    }

    if(!isset($where_string)) $where_string = '';

    /*
     * End of Where Section
     */


    /*
     * Build SQL
     */
    $SQL = $select_string.'from '.$schema.'.'.$table.$where_string;

    //...continue to execute query and echo results
?>

So as you can see, every part of the where array gets escaped and added to the query, even the column names and the operators (=, !=, >, <, etc).

Also as you can see it seems little crazy they I am passing separate strings for every part of this part of the where clause -> and STR_TO_DATE(date, "%m/%d/%Y" ) > (date(now()) - INTERVAL 30 day)

If you all have

  1. experience with passing dynamic where clauses to the ajax script
  2. if you have a better idea
  3. see a security hole in the scripts

please let me know. hopefully we can get a nice, all purpose data basing script going.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:13:14+00:00Added an answer on June 9, 2026 at 9:13 pm

    Personally I do not recommend this kind of approach where you expose the Database column names in the javascript, which is completely dangerous.

    Simple example is you can manipulate the ajax parameters in browser level and execute it against your database. You are simply enabling the hackers to get more information about your data.

    For example I could stop the where close so that i can see all your data from your database by executing a call without where parameters. If you want to develop such dynamic queries, make sure you are using some mappings between the ajax passed parameters and the real table columns so that the columns are not exposed to the hackers directly.

    Thats the basic security issue that I could see in this script.

    Hope this helps.

    Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to build a dynamic gallery that is loaded via AJAX. I'm trying to
Trying to build dynamic output from json and using jq/template tmpl display rows/columns. Somehow
Trying to build the following simple example #include <boost/python.hpp> using namespace boost::python; tuple head_and_tail(object
Im trying to build a feeds application in php in which im using a
I'm trying to find an easy way to build forms which show dates in
I'm trying build a conditions array to be using in a prepared statement: Cars.find(:all,
im trying to build a application or a method that goes through all of
I trying to build a dynamic query similar to: def domain = DomainName def
I'm trying build an sort of property set in ksh. Thought the easiest way
Trying to build a function that can insert into a SQL database regardless of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.