2013-11-26 111 views
1

我使用的是Trirand.com的jqgrid,我無法通過內置的表單對話框添加或編輯數據。當我編輯和/或添加數據並點擊提交數據刷新,但沒有任何變化。我檢查了PHPmyadmin,沒有數據被更改或添加到數據庫。Jqgrid無法添加或編輯數據

任何幫助,將不勝感激。

EDIT

HTML/jQuery代碼

jQuery(document).ready(function(){ 
      jQuery("#grid").jqGrid("initFontAwesome").jqGrid({ 
       pager:'#pager', 
       url:'getunits.php', 
       datatype: "json", 
       mtype: 'GET', 
       height: 300, 
       width: 1000, 
       colNames:['Unit Id', 'Div Id', 'Title Org', 'Short Desc', 'Long Desc', 'Comments', 'Enabled'], 
       colModel:[ 
         {name:'unit_id', 
          index:'unit_id', 
          width:40, 
          editable:true, 
          sortable:false, 
          resizable:false, 
          align:"center", 
          editoptions:{size:7,maxlength:6}, 
          editrules:{required:true,number:true}, 
          formoptions:{elmprefix:"(*)"} 
         }, 
         {name:'div_id', 
          index:'div_id', 
          width:30, 
          editable:true, 
          sortable:false, 
          resizable:false, 
          align:"center", 
          editoptions:{size:4,maxlength:4}, 
          editrules:{required:true,number:true}, 
          formoptions:{elmprefix:"(*)"} 
         }, 
         {name:'title_org', 
          index:'title_org', 
          width:60, 
          editable:true, 
          sortable:false, 
          resizable:false, 
          editoptions:{size:11,maxlength:11}, 
          editrules:{required:true,number:true}, 
          formoptions:{elmprefix:"(*)"} 
         }, 
         {name:'short_desc', 
          index:'short_desc', 
          width:50, 
          editable:true, 
          resizable:false, 
          sortable:false, 
          editoptions:{size:7,maxlength:10}, 
          editrules:{required:true}, 
          formoptions:{elmprefix:"(*)"} 
         }, 
         {name:'long_desc', 
          index:'long_desc', 
          width:230, 
          editable:true, 
          resizable:false, 
          sortable:false, 
          edittype:"textarea", 
          editoptions:{rows:"2",cols:"30",maxlength:60}, 
          editrules:{required:true}, 
          formoptions:{elmprefix:"(*)"} 
         }, 
         {name:'unit_desc', 
          index:'unit_desc', 
          width:230, 
          editable:true, 
          resizable:false, 
          sortable:false, 
          edittype:"textarea", 
          editoptions:{rows:"2",cols:"30",maxlength:60} 
         }, 
         {name:'avail_ind', 
          index:'avail_ind', 
          width:40, 
          editable:true, 
          resizable:false, 
          sortable:false, 
          align:"center", 
          edittype:"select", 
          editoptions:{value:"Y:Y;N:N"} 
         } 
        ], 

       pager: jQuery('#gridpager'), 
       viewrecords: true, 
       sortorder: "asc", 
       sortname: "div_id", 
       caption:"Unit Codes", 
       editurl:'getunits.php', 
       toppager:true, 
       recordtext:'', 
       hidegrid:false, 
       scroll:true, 
       rowNum:"10000" 

     }); 

     jQuery("#grid").jqGrid('navGrid','#grid_toppager', 
     { 

      add:true, 
      edit:true, 
      view:true, 
      search:false, 
      del:false, 
      refresh:false 
     }, 

     { // Edit form 
      width:"auto", 
      height:"auto", 
      top:220, 
      left:500, 
      viewPagerButtons:false, 
      topinfo:"Fields marked with (*) are required", 
      resize:false 
     }, 

     { // Add form 
      width:"auto", 
      height:"auto", 
      top:220, 
      left:500, 
      topinfo:"Fields marked with (*) are required", 
      resize:false, 
      reloadAfterSubmit:true, 
      closeAfterAdd: true 
     }, 

     { // prmDel 

     }, 

     { //prmSearch 

     }, 

     { //prmView 
      top:220, 
      left:460 
     } 


     ); 


     }); // jQuery("#grid").jqGrid 
</script> 

PHP代碼

<?php 

$dbhost = "localhost"; 
$dbuser = "root"; 
$dbpass = "*******"; 
$dbname = "fdmamaint"; 


$page = $_GET['page']; // get the requested page 
$limit = $_GET['rows']; // get how many rows we want to have into the grid 
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
$sord = $_GET['sord']; // get the direction 

if(!$sidx) $sidx =1; 

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

mysql_select_db($dbname) or die("Error conecting to db."); 
$result = mysql_query('SELECT unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind FROM depunits where avail_ind="Y" and active_ind="Y"'); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 
$count = $row['count']; 

if($count >0) 
{ 
    $total_pages = cell($count/$limit); 
} 
else 
{ 
     $total_pages = 0; 
} 

if ($page > $total_pages) $page=$total_pages; 
$start = $limit*$page - $limit; // do not put $limit*($page - 1) 
$SQL = 'SELECT unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind FROM depunits where avail_ind="Y" and active_ind="Y"'; 
$result = mysql_query($SQL) or die("Couldn t execute query.".mysql_error()); 

$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[unit_id]; 
    $response->rows[$i]['cell']=array($row[unit_id],$row[div_id],$row[title_org],$row[short_desc],$row[long_desc],$row[unit_desc],$row[avail_ind]); 
    $i++; 
}   
echo json_encode($response); 

?> 

修訂

EditDivisions.php

<?php 

$dbhost = "localhost"; 
$dbuser = "root"; 
$dbpass = "*****"; 
$dbname = "fdmamaint"; 

// connect to the database 
$mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error()); 

mysql_select_db($dbname) or die("Error conecting to db."); 

$div = mysql_real_escape_string($_GET['div_id']); 
$l2 = mysql_real_escape_string($_GET['l2_id']); 
$l1l2 = mysql_real_escape_string($_GET['l1l2_id']); 
$l1l3 = mysql_real_escape_string($_GET['l1l3_id']); 
$l2l3 = mysql_real_escape_string($_GET['l2l3_id']); 
$beg = mysql_real_escape_string($_GET['exec_beg']); 
$end = mysql_real_escape_string($_GET['exec_end']); 
$csa = mysql_real_escape_string($_GET['csa_id']); 
$area = mysql_real_escape_string($_GET['area_id']); 
$areadesc = mysql_real_escape_string($_GET['area_desc']); 
$shortdesc = mysql_real_escape_string($_GET['short_desc']); 
$longdesc = mysql_real_escape_string($_GET['long_desc']); 
$enabled = mysql_real_escape_string($_GET['avail_ind']); 

if($_GET['oper']=='add'){ 

    mysql_query("INSERT INTO depdivisions (div_id, l1l2_id, l2_id, l1l3_id, l2l3_id, exec_beg, exec_end, csa_id, area_id, area_desc, short_desc, long_desc, avail_ind) values ($div,$l1l2,$l2,$l1l3,$l2l3,$beg,$end,$csa,$area,$areadesc,$shortdesc,$longdesc,$enabled"); 

}else if($_GET['oper']=='edit'){ 

}else if($_GET['oper']=='del'){ 

} 

?> 
+0

你能不能請添加你的代碼 – Ayaz

+0

你只需在你的添加/編輯php代碼中選擇?你的插入和更新代碼在哪裏? – kmas

+0

@kmas我的印象是,在網格基本代碼中內置了處理它們的函數。難道它不在jquery.jqGrid.src.js中嗎? – JCooper

回答

0

要回答JCooper評論(I was under the impression that there are built in functions inside the grid base code that handles them. Wouldn't it be inside the jquery.jqGrid.src.js?)。

它是如何工作的:

在你編輯的PHP網頁/文件(getunits.php,不知道這是一個很好的選擇,因爲它是相同的URL作爲你的「load_data」 URL),你必須檢查,如果用戶添加,編輯或刪除:

// Here, we use $_GET, because you ask for it in configuration. 
$unit_id = $_GET['unit_id']; // And so on for the other form values 

if($_GET['oper']=='add'){ 
    // mysql insert 
}else if($_GET['oper']=='edit'){ 
    // mysql update 
}else if($_GET['oper']=='del'){ 
    // mysql delete 
} 

當操作完成後,網格再次通過「load_data」URL加載。

+0

如何引用用戶數據以傳入插入語句值? – JCooper

+0

在colModel數組中,你使用'name:'unit_id'' ==>你可以用'$ _GET ['unid_id']'得到這個值。 – kmas

+0

我已更新我的原始文章以包含我的編輯文件。當我嘗試添加行時,我現在正在收到「內部服務器錯誤500」。 – JCooper

相關問題