数据表使用 ssp.class.php 放弃语法错误警告
datatables 的 class.php 出现如下错误
错误:
An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY request_id ASC↵ LIMIT 0, 10' at line 4
我的服务器端代码如下,名为 request-list.php
<?php
$inc_files = array("ph_utils");
require_once ('autoload.php');
$db = new db();
$table = 'wallet_load_request';
$primaryKey = 'request_id';
$columns = array(
array('db' => 'request_id', 'dt' => 0),
array('db' => 'request_date', 'dt' => 1),
array('db' => 'trans_mode', 'dt' => 2),
array('db' => 'trans_amt', 'dt' => 3),
array('db' => 'request_status', 'dt' => 4),
array('db' => 'org_id', 'dt' => 5)
);
$sql_details = array(
'user' => PH_DB_USER,
'pass' => PH_DB_PSWD,
'db' => PH_DB,
'host' => PH_DB_HOST
);
require_once('ssp.class.php');
$filterByStaus = isset($_REQUEST['filter_by_status']) ?
$_REQUEST['filter_by_status'] : "PENDING";
switch ($filterByStaus) {
case 'REJECTED':
$fixedFilter .= " AND request_status = 'REJECTED'";
break;
case 'APPROVED':
$fixedFilter .= " AND request_status = 'APPROVED'";
break;
default:
break;
}
$output = SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns,
$fixedFilter);
foreach ($output['data'] as $i => $d) {
$request_id = $output['data'][$i][0];
$trans_amt = $output['data'][$i][3];
$request_status = $output['data'][$i][4];
$org_id = $output['data'][$i][5];
$output['data'][$i][0] = '<div class="text-center"><h4><span class="label label-success"> #'. $request_id .'</span></h4></div>';
if ($request_status == 'APPROVED') {
$output['data'][$i][4] = '<div class="text-center"><span class="label label-success"> APPROVED </span></div>';
$output['data'][$i][5] = '<div class="text-center"><button id="accept" type="button" class="text-center btn btn-success accept disabled" data-request_id=\'' . $request_id . '\' data-org_id=\'' . $org_id . '\' data-trans_amt=\'' . $trans_amt . '\'><i class="fa fa-ban"></i> ACCEPT</button></div>';
$output['data'][$i][6] = '<div class="text-center"><button id="reject" type="button" class="text-center btn btn-danger reject disabled" data-request_id=\'' . $request_id . '\'><i class="fa fa-ban"></i> REJECT</button></div>';
} else if ($request_status == 'REJECTED') {
$output['data'][$i][4] = '<div class="text-center"><span class="label label-danger"> REJECTED </span></div>';
$output['data'][$i][5] = '<div class="text-center"><button type="button" id="accept" class="text-center btn btn-success accept disabled" data-request_id=\'' . $request_id . '\' data-org_id=\'' . $org_id . '\' data-trans_amt=\'' . $trans_amt . '\'><i class="fa fa-ban"></i> ACCEPT</button></div>';
$output['data'][$i][6] = '<div class="text-center"><button type="button" id="reject" class="text-center btn btn-danger reject disabled" data-request_id=\'' . $request_id . '\'><i class="fa fa-ban"></i> REJECT</button></div>';
} else {
$output['data'][$i][4] = '<div class="text-center"><span class="label label-warning"> PENDING </span></div>';
$output['data'][$i][5] = '<div class="text-center"><button type="button" id="accept" class="text-center btn btn-success accept" data-request_id=\'' . $request_id . '\' data-org_id=\'' . $org_id . '\' data-trans_amt=\'' . $trans_amt . '\'><i class="fa fa-ban"></i> ACCEPT</button></div>';
$output['data'][$i][6] = '<div class="text-center"><button type="button" id="reject" class="text-center btn btn-danger reject" data-request_id=\'' . $request_id . '\'><i class="fa fa-ban"></i> REJECT</button></div>';
}
}
SSP::sendJson($output);
我的 jquery 代码如下 request-list.js
var requestList = {
oTable: {},
init: function () {
requestList.oTable = $('#req_list').dataTable({
"processing": true,
"serverSide": true,
"columnDefs": [{className: "data_middle ", "targets": [0, 1, 2, 3, 4, 5, 6]}],
"columns": [null, null, null, {"orderable": false}, {"orderable": false}, {"orderable": false}, {"orderable": false}],
"ajax": {
"url": "web/request-list",
"data": function (d) {
return $.extend({}, d, {
"filter_by_status": $('#status').val(),
});
}
}
});
}
}
我的前端代码是 list.html
筛选依据: 待处理 已批准 已拒绝
<br>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<table class="table table-bordered table-striped table-hover" id="req_list">
<thead>
<tr>
<th>Request ID</th>
<th>DATE</th>
<th>Mode</th>
<th class="text-center">AMOUNT</th>
<th class="text-center">STATUS</th>
<th class="text-center">ACCEPT</th>
<th class="text-center">REJECT</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
运行代码后,页面显示警告,如下所示 DataTables 警告:table id=req_list - 发生 SQL 错误:SQLSTATE[42000]:语法错误或访问违规:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解第 4 行“ORDER BY request_id ASC LIMIT 0, 10”附近用户的正确语法
在控制台中我收到错误
jquery.dataTables.min.js:39 Uncaught TypeError: Cannot read property 'length' of undefined
自定义 ssp 代码如下,位于以下链接中
https://drive.google.com/open?id=0B-e2dOqifK3QOFFnRjhTYjhPQmM
如果在这种情况下您想要过滤数据,则需要使用
complex()
而不是
simple()
方法。
Complex() 方法
SSP::complex($request, $conn, $table, $primaryKey, $columns, $whereResult = null, $whereAll = null)
Simple() 方法
simple($request, $conn, $table, $primaryKey, $columns)
如果它不适合您,请告诉我们。
您不需要在
$fixedFilter
变量的值中使用前导
AND
。请参阅以下更正后的摘录:
switch ($filterByStaus) {
case 'REJECTED':
$fixedFilter = "request_status = 'REJECTED'";
break;
case 'APPROVED':
$fixedFilter = "request_status = 'APPROVED'";
break;
default:
break;
}
我只需要更改以下代码
switch ($filterByStaus) {
case 'REJECTED':
$fixedFilter .= " AND request_status = 'REJECTED'";
break;
case 'APPROVED':
$fixedFilter .= " AND request_status = 'APPROVED'";
break;
default:
break;
我正在附加查询,因此我尝试在 $fixedFilter 之后删除 .
switch ($filterByStaus) {
case 'REJECTED':
$fixedFilter = " AND request_status = 'REJECTED'";
break;
case 'APPROVED':
$fixedFilter = " AND request_status = 'APPROVED'";
break;
default:
break;