我需要更改sql语句中的“where-clause”,该语句用于从数据库获取数据并将其显示在dataTables中,方法是更改myInput的值并将其传递到服务器端。我有这个代码:
<input id=myInput value="london">
<script>
$(document).ready(function(){
$('#example').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "server_processing.php",
"data": function ( d ) {
d.myInput = $('#myInput').val();
}
}
});
} );
</script>
服务器端有这样的代码:
<?php
//...HERE I have defined these: $sql_details, $table, $primaryKey, $columns
$where="`office`='$_POST['myInput']'";
require( 'ssp.class.php' );
echo json_encode(SSP::simple($_GET,$sql_details,$table,$primaryKey,$columns,$where));
我哪里出错了?我已经根据许多示例进行了尝试,并将其发布在互联网上,但最终得到了相同的结果。
最佳答案
您的 PHP 脚本存在一些问题。
- 您在
$_POST['myInput']
中使用带引号的键,不带花括号 语法。 - 您无法逃避会引发 SQL 注入(inject)攻击的
$_POST['myInput']
。 SSP::simple
不接受包含WHERE
子句的参数,您需要使用SSP::complex
代替。
更正后的代码如下所示:
<?php
//...HERE I have defined these: $sql_details, $table, $primaryKey, $columns
require( 'ssp.class.php' );
$conn = SSP::db($sql_details);
$where="office=".$conn->quote($_POST['myInput']);
echo json_encode(SSP::complex($_GET,$conn,$table,$primaryKey,$columns,$where));
关于javascript - 数据表: passing extra data to php server-side script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30405962/