0

jquery builder(来自http://querybuilder.js.org/)用于让用户通过 PHP 函数选择日期并进一步选择 DataTables (datatables.net/ ) 的数据。

DataTables 尤其是 Ajax 函数如下所示:

var table = $(id).DataTable({
        serverSide: true,
        searching: true,
        processing: true,,
        ajax: {
            url: "controllers/myAjax.php",
            type: "POST",
            data: result
        }
});

作为数据传递的对象由 queryBuilder 定义,并附加到我在 PHP 脚本中的查询字符串中。为了确定事情,我将数据作为纯 SQL ( http://querybuilder.js.org/plugins.html#import-export ) 传递。在我的问题测试用例中,这是:

WHERE birthdate < '1990-01-01' 

这将导致 SELECT 查询:

SELECT * from table_1 WHERE birthdate < '1990-01-01' 

此查询引发 MySQL 错误:

"[...] check the manual that corresponds to your MySQL 
server version for the right syntax to use near '\'1990-01-01\' "

显然日期没有正确转义。但是当我将这个查询准确地输入到我的 MySQL 工作台时,服务器会执行并返回一组正确的结果。更重要的是,工作台并不关心我是使用单引号 (') 还是双引号 (")。

此外,我尝试使用 PHP str_replace 手动删除这些转义字符。然后该函数返回值,但显然被解释为 int 并破坏其他查询(如相等 ID)。msqli.real-escape-string ( http://php.net/manual/de/mysqli.real-escape-string.php ) 也是如此。

我尝试的另一种方法是稍微更改 Ajax 函数的 dataType - 但基本上我发送的是表单编码数据,所以默认类型应该没问题?

那么为什么(仅)日期字段以错误的方式转义?在我必须编写自己的 PHP 函数来访问数据库之前,是否有任何相当快速的解决方法?

4

0 回答 0