Php 多列上的数据表排序工作不正常
我在数据表的排序行为方面遇到了一些困难。排序是可行的,但它只是短了“ID”列。当我尝试按任何列排序时,它将根据包含自动递增值的ID列进行排序。 以下是Jscode代码:Php 多列上的数据表排序工作不正常,php,mysql,datatables,Php,Mysql,Datatables,我在数据表的排序行为方面遇到了一些困难。排序是可行的,但它只是短了“ID”列。当我尝试按任何列排序时,它将根据包含自动递增值的ID列进行排序。 以下是Jscode代码: var datatables = $('#plans_data').DataTable({ "Processing":true, "serverSide":true, "order":[], "ajax":{ url:"../controller/fetch.php",
var datatables = $('#plans_data').DataTable({
"Processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"../controller/fetch.php",
type:"POST"
},
"lengths":[10,50,50,100],
"columnDefs":[
{
"targets":[0,8],
"orderable":false,
},
],
});
我有一个php文件,其中存储了所有查询。以下是php文件代码的一部分:
$query = '';
$output = array();
$query .= "SELECT * FROM plans ";
if(isset($_POST["order"]))
{
$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= 'ORDER BY ID ASC ';
}
if($_POST["length"] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row["ID"];
$sub_array[] = $row["num_clas"];
$sub_array[] = $row["num_tf"];
$sub_array[] = $row["num_maj"];
$sub_array[] = $row["nom_p"];
$sub_array[] = $row["nom_Geom"];
$sub_array[] = $row["categorie"];
$sub_array[] = $row["superficie"];
$sub_array[] = $row["arrondis"];
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => $data
);
echo json_encode($output);
?>```
When I click on the sorting icon, it sorts but only on the "ID" column. I dont really understand why and I've tried to fine some issues but all went wrong.
如果它仅在ID字段上排序,则您的代码可能无法识别您的
$\u POST
,并执行以下行:$query.='ORDER BY ID ASC'代码>。尝试执行echo$查询
以查看正在形成和运行的查询。我已尝试使用echo$query;获取$query的内容;。这是从计划中选择的结果,SELECT*其中num_clas像“%%”或num_tf像“%%”或num_maj像“%%”或nom_p像“%%”或nom_Geom像“%%”或arrondis像“%%”**按3 asc排序**
,因此它似乎只需要对列的索引进行排序。我不知道从哪里可以得到这个结果。所有的like
条件来自哪里?我在你的密码上看不到。此外,您的ORDER BY可能获取了错误的$\u POST
变量。按3进行排序可能不是您想要的。此外,像“%”这样执行也没有意义,因为它不排除任何内容。我最终通过比较索引并将其替换为真实列名解决了问题,效果很好。您的代码允许SQL注入攻击。将您的参数添加到prepare调用中,以便库能够处理它。