2011-12-02 114 views
1

我是一個jQuery/jqGrid的新手...如何添加/編輯/刪除MySQL數據庫與jqGrid的

我有我的jqGrid選擇和正確地顯示我的數據。我也有編輯/添加按鈕在我的網格上添加/編輯數據。但是,我不知道如何讓這些添加/編輯更新MySQL數據庫。有什麼建議麼?

這裏是我的scripts.js中的文件:

$("#table-maintenance-conflicts").jqGrid(
    { 
     url:'maintenanceConflicts.php', 
     editurl:'maintenanceConflicts-edit.php', 
     datatype: 'xml', 
     mtype: 'GET', 
     colNames:['Conflict Code', 'Description', 'Updated By', 'Updated On'], // Set column names of the grid 
     colModel :[ // Set the settings for each individual column of the grid 
      { 
       name:'conflictCode', 
       index:'conflictCode', 
       width:120, 
       align:'center', 
       editable:false, 
      }, 
      {name:'description', index:'description', width:200, editable:true, edittype:"text"}, 
      {name:'updatedBy', index:'updatedBy', width:120, editable:true, edittype:"text", editrules: {required: true}}, 
      {name:'updatedOn', index:'updatedOn', width:120, editable:false, editrules: {required: true}} 
     ], 
     pager: '#pager-maintenance-conflicts', 
     rowNum:10, // To start, the user will see 10 records on the first page 
     rowList:[10,15,20], // How many records is the user able to see per page? 
     sortname: 'conflictCode', 
     sortorder: 'desc', // Order the 'sortname' should be sorted in 
     viewrecords: true, // View the records upon grid startup? or wait until a search? 
     caption: 'Maintenance | Conflicts' // Title of the grid (at the top) 
    }).navGrid('#pager-maintenance-conflicts',{ 
     edit:true, 
     add:true, 
     view:true, 
     del:true, 
     search:true, 
     refresh:true 
    }); 

// ADD 
$("#add_table-maintenance-conflicts").click(function(){ 

    jQuery("#table-maintenance-conflicts").jqGrid('editGridRow',"new",{ 
     mtype: 'POST', 
     reloadAfterSubmit:false, 
     closeAfterAdd:true,   
    }); 

}); 

// EDIT 
$("#edit_table-maintenance-conflicts").click(function(){ 

    var gr = jQuery("#table-maintenance-conflicts").jqGrid('getGridParam','selrow'); 
    if(gr != null) 
     jQuery("#table-maintenance-conflicts").jqGrid('editGridRow',gr,{ 
      mtype: 'POST', 
      reloadAfterSubmit:false, 
      closeAfterEdit:true 
     }); 

}); 

// DELETE 
$("#del_table-maintenance-conflicts").click(function(){ 

    var gr = jQuery("#table-maintenance-conflicts").jqGrid('getGridParam','selrow'); 
    if(gr != null) 
     jQuery("#table-maintenance-conflicts").jqGrid('delGridRow',gr,{ 
      mtype: 'POST', 
      reloadAfterSubmit:false 
     }); 

}); 

這裏是我的maintenanceConflicts-edit.php文件:

<?php 

//include the information needed for the connection to MySQL data base server. 
// we store here username, database and password 
$dbhost = "localhost"; 

$dbuser = "root"; 

$dbpassword = ""; 

$database = "ftdbadmin"; 

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

// select the database 
mysql_select_db($database) or die("Error conecting to db."); 
//echo "Connected to the selected database: ".$database."<br />"; 

// If we're ADDING 
if($_POST['oper']=='add') 
{ 

} 

// If we're EDITING 
if($_POST['oper']=='edit') 
{ 
    $conflictCode  = mysql_real_escape_string($_POST['conflictCode']); 
    $description = mysql_real_escape_string($_POST['description']); 
    $updatedBy = mysql_real_escape_string($_POST['updatedBy']); 
    // $updatedOn = mysql_real_escape_string($_GET['updatedOn']); 

$SQL = "UPDATE tblconflict SET updatedBy = '".$updatedBy."', description = '".$description."' WHERE conflictCode = ".$conflictCode; 

    echo $SQL; 
    $result=mysql_query($SQL) or die(mysql_error()); 

    mysql_close($db); 
} 

// If we're DELETING 
if($_POST['oper']=='del') 
{ 

} 

?> 

任何幫助將不勝感激!

+1

檢查此鏈接http://www.trirand.com/blog/?p=617 – 2011-12-02 15:55:44

+0

謝謝安德烈,但該博客文章看起來像它專注於商業產品,PHP的jQGrid ... – FastTrack

+0

這是非常奇怪的我..我一直在搜索無處不在,找不到任何如何實現與我的網格添加/編輯/刪除功能的任何好例子...任何人都有他們知道的任何示例或教程? – FastTrack

回答

-2

幾個月前,我在Wiki,Google和這個網站上找到了這個數據。我偶然發現了jQGrid的這個PHP CRUD模板。稍微調整一下,我就可以使用我所有的網格。一旦你開始更多地使用它,你會注意到很多改進和擴展的空間。它應該讓你開始正確的方向。

+0

請考慮直接在您的答案中提供解決方案。事實上,它符合不符合SO標準的僅鏈接答案。 – BartoszKP

+0

@BartoszKP鏈接是答案。爲什麼我會不必要地複製/粘貼鏈接的內容? – FastTrack

+1

這不是不必要的,因爲鏈接可能會失效。它目前正在制定SO政策。例如,請閱讀[this](http://meta.stackexchange.com/a/95691/231717),如果您真的對此感興趣,可能還有大量的其他meta討論。無論哪種方式,共識是隻有鏈接的答案是不受歡迎的。 – BartoszKP

相關問題