jQgrid搜索返回where子句不起作用的所有记录

时间:2014-01-08 18:03:24

标签: php mysql search jqgrid

我有一个带有工具栏(navGrid)的简单jQgrid,用于添加,编辑,删除和搜索。问题是搜索不起作用。它似乎总是从表中返回所有记录而不是一个,这意味着where子句有问题,我试图根据搜索模式窗口中给出的内容生成。或者可能根本没有激活搜索案例..

这是网格代码:

$("#list").jqGrid({
        url: "server.php",
        datatype: "json",
        mtype: "POST",
        colNames: ["Driver ID", "Name", "Country", "Victories", "Poles",     
"Titles", "Fastest laps"], //"Team name"],
        colModel: [
            { name: "driverid", index:"driverid", width: 55, search:true,             stype:'text', editable:false},
        { name: "name", index:"name", width: 90, search:true, stype:'text', editable:true },
        { name: "country", index:"country", width: 90, search:true, stype:'text', editable:true },
        { name: "victories", index:"victories", width: 80, search:true, align: "right", editable:true },
        { name: "poles", index:"poles", width: 80, search:true, align: "right", editable:true },
        { name: "titles", index:"titles", width: 80, search:true, align: "right", editable:true },
        { name: "flaps", index:"flaps", width: 80, search:true,  align: "right", editable:true}
        //{ name: "teamName", width: 80, align: "right", editable:true}
        ],
        autowidth: true,
        pager: "#pager",
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: "driverid",
        sortorder: "asc",
        viewrecords: true,
        gridview: true,
        autoencode: true,
        caption: "F1 statistics grid",
        editurl: "dbedit.php"
    }); 
jQuery("#list").jqGrid('navGrid','#pager',{add:true,del:true,edit:true,search:true});
}); 

以下是dbedit.php中的部分:

include("connection.php"); //

$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); 

// select the database 
mysql_select_db($database) or die("Error connecting to db."); 


$action="";
if (isset($_POST['oper']))
{
$action=$_POST['oper'];

}
else
{
echo "Definisi akciju";
exit();
}

switch ($action)
{

case "add":
{

    $driverid  =   mysql_real_escape_string($_POST['driverid']); 
    $name  =   mysql_real_escape_string($_POST['name']); 
    $country =   mysql_real_escape_string($_POST['country']); 
    $victories    =   mysql_real_escape_string($_POST['victories']);
    $poles   =   mysql_real_escape_string($_POST['poles']);      
    $titles  =   mysql_real_escape_string($_POST['titles']); 
    $flaps   =   mysql_real_escape_string($_POST['flaps']);
    //$teamName   =   mysql_real_escape_string($_POST['teamName']);  
    //$insert = 'INSERT INTO '.'drivers';  
    //$insert .= ' VALUES (NULL,';
        //$insert .="'".$name."','".$country."', '".$victories."','".$poles."', '".$titles."', '".$flaps."')";

    $insert = "INSERT INTO drivers (driverid, name, country, victories, poles, titles, flaps) VALUES (Null,'".$name."','".$country."','".$victories."','".$poles."','".$titles."','".$flaps."')";



    $result = mysql_query( $insert ) or die("Couldn't execute query.".mysql_error()); 
    //$_SESSION['nesto'] = $result;
}
break;

case "edit":
{
    $driverid  =   mysql_real_escape_string($_POST['id']);
    $name  =   mysql_real_escape_string($_POST['name']); 
    $country =   mysql_real_escape_string($_POST['country']); 
    $victories    =   mysql_real_escape_string($_POST['victories']);
    $poles   =   mysql_real_escape_string($_POST['poles']);      
    $titles  =   mysql_real_escape_string($_POST['titles']); 
    $flaps   =   mysql_real_escape_string($_POST['flaps']);  
    $update = 'UPDATE drivers'." SET name='". $name ."', country='" . $country ."', victories='" . $victories ."', poles='" . $poles ."', titles='" . $titles ."', flaps='" . $flaps. "' WHERE driverid=". $driverid ;
//echo $update;
    $result = mysql_query( $update ) or die("Couldn't execute query.".mysql_error()); 
}


break;
case "del":
{

$driverid  =   mysql_real_escape_string($_POST['id']); //id reda iz grida
$delete = 'DELETE FROM drivers WHERE driverid='.$driverid;
$result = mysql_query( $delete ) or die("Couldn't execute query.".mysql_error()); 

}
break;

case "search":
{
$page = $_POST['page']; 

// get how many rows we want to have into the grid - rowNum parameter in the grid 
 $limit = $_POST['rows']; 
 //echo $limit;
// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel 
$sidx = $_POST['sidx']; 

// sorting order - at first time sortorder 
$sord = $_POST['sord']; 

// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1; 





$ops = array(
'eq'=>'=', //equal
'ne'=>'<>',//not equal
'lt'=>'<', //less than
'le'=>'<=',//less than or equal
'gt'=>'>', //greater than
'ge'=>'>=',//greater than or equal
'bw'=>'LIKE', //begins with
'bn'=>'NOT LIKE', //doesn't begin with
'in'=>'LIKE', //is in
'ni'=>'NOT LIKE', //is not in
'ew'=>'LIKE', //ends with
'en'=>'NOT LIKE', //doesn't end with
'cn'=>'LIKE', // contains
'nc'=>'NOT LIKE'  //doesn't contain
);

function getWhereClause($col, $oper, $val){
global $ops;
//if($oper == 'eq' || $oper == 'ne') $val .= "";
if($oper == 'bw' || $oper == 'bn') $val .= '%';
if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
return " WHERE $col {$ops[$oper]} '$val' ";
}

$where = ""; //if there is no search request sent by jqgrid, $where should be empty
$searchField = isset($_POST['searchField']) ? $_POST['searchField'] : false;
$searchOper = isset($_POST['searchOper']) ? $_POST['searchOper']: false;
$searchString = isset($_POST['searchString']) ? $_POST['searchString'] : false;

if ($_POST['_search'] == 'true') {
$where = getWhereClause($searchField,$searchOper,$searchString);
}

// connect to the MySQL database server 
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " .     mysql_error()); 

// select the database 
mysql_select_db($database) or die("Error connecting to db."); 

// calculate the number of rows for the query. We need this for paging the result 
$result = mysql_query("SELECT COUNT(*) AS count FROM drivers ".$where.")";
//$result = mysql_query("SELECT COUNT(*) AS count FROM drivers a, teams b WHERE     "."a.driverid=b.driver1id".""); 
//echo $result1;
$row = mysql_fetch_array($result,MYSQL_ASSOC);  
$count = $row['count']; 
//$_SESSION['nesto'] = $count;
// calculate the total pages for the query 
if( $count > 0 && $limit > 0) { 
$total_pages = ceil($count/$limit); 
} else { 
$total_pages = 0; 
} 

// if for some reasons the requested page is greater than the total 
// set the requested page to total page 
if ($page > $total_pages) $page=$total_pages;

// calculate the starting position of the rows 
$start = $limit*$page - $limit;

// if for some reasons start position is negative set it to 0 
// typical case is that the user type 0 for the requested page 
if($start <0) $start = 0; 

// the actual query for the grid data 

 $SQL = "SELECT driverid, name, country, victories, poles, titles, flaps FROM drivers ".$where;
 //$SQL = "SELECT "."a.driverid, a.name, a.country, a.victories, a.poles, a.titles,  a.flaps, b.teamName"." FROM drivers a, teams b WHERE a.driverid=b.driver1id OR  a.driverid=b.driver2id ORDER BY $sidx $sord LIMIT $start , $limit";


//echo $SQL;
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 
//$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());


$response=null;
$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$response->rows[$i]['id']=$row['driverid'];
$response->rows[$i] ['cell']=array($row['driverid'],$row['name'],$row['country'],$row['victories'],$row['poles'],$row['titles'],$row['flaps']);//,$row['teamName']);
$i++;
}        
echo json_encode($response);

}

我已经完成了20次代码,看看我输错了什么,我看不到它。当我定义网格或colModel时,我错过了什么?

1 个答案:

答案 0 :(得分:0)

我已经失去了很多时间试图解决这个问题。最后,我在网格代码中放置了loadonce:true并且它有效。所以,试试吧。把它放在任何地方,例如:viewrecords:true