douzhi1813 2010-09-24 09:23
浏览 611
已采纳

datatable:make服务器端脚本,用于过滤每列

i want to make my table can filtering in each column. i get client side script from DataTable.net. To make this work, i must make change at server side script for this datatable. My table fields is: Line, Model_name, Version, Lot, Serial_number, ID_number, Lot_no_S_Serial, and Prod_date.

i have tried to syncronizing each column to this server script, but i always get an error. this the following script:

sSearch:
bEscapeRegex:true
sSearch_0:
bEscapeRegex_0:true
bSearchable_0:true
sSearch_1:
bEscapeRegex_1:true
bSearchable_1:true
sSearch_2:
bEscapeRegex_2:true
bSearchable_2:true   //data array same until sSearch_7

EDIT

and this the query:

$sWhere = "";
if (postVar('sSearch') !="" )
{
  $sWhere = " WHERE Line LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%'  ";
}
if (postVar('sSearch_0') !="")
{
  $sWhere = " AND Line LIKE '".mysql_real_escape_string( $_POST['sSearch_0'])."' ";
}
if (postVar('sSearch_1') !="") 
{ 
  $sWhere = " AND Model_name LIKE '%".mysql_real_escape_string( $_POST['sSearch_1'])."%' ";

//until sSearch_7

i'm getting error at this query:

error: "Error occuered during query execution: (): 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 'AND Line LIKE '2' ORDER BY Model_name desc LIMIT 0, 10' at line 1";

  • 写回答

3条回答 默认 最新

  • dongqucheng3851 2010-09-24 10:30
    关注

    you have to chain your queries.
    while you are replacing $sWhere with each further condition.
    you can use concatenation (.= instead of =), but let me suggest to use array for this:

    $aWhere = array();
    if (postVar('sSearch')) {
      $aWhere[] = "Line LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%'";
    }
    if (postVar('sSearch_0'))
    {
      $aWhere[] = "Line LIKE '".mysql_real_escape_string($_POST['sSearch_0'])."'";
    }
    if (postVar('sSearch_1')) 
    { 
      $aWhere = "Model_name LIKE '%".mysql_real_escape_string( $_POST['sSearch_1'])."%'";
    }
    //and so on
    if (count($aWhere)) $where="WHERE ".implode(' AND ',$aWhere);
    $query="select * from table $where";
    mysql_query($query) or trigger_error(mysql_error().": ".$query);
    

    the last line of this code will print out not only error message but also query itself which will be extremely helpful

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路