Php 如何使用jqGrid在MySQL数据库中添加/编辑/删除

Php 如何使用jqGrid在MySQL数据库中添加/编辑/删除,php,jquery,jqgrid,Php,Jquery,Jqgrid,我是jQuery/jqGrid的新手 我已经让jqGrid正确地选择和显示了我的数据。我也有编辑/添加按钮在我的网格上添加/编辑数据。然而,我不确定如何使这些添加/编辑更新MySQL数据库。有什么建议吗 这是我的scripts.js文件: $("#table-maintenance-conflicts").jqGrid( { url:'maintenanceConflicts.php', editurl:'maintenanceConflicts-e

我是jQuery/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')
{

}

?>

几个月前,我通过在维基、谷歌和这个网站上的搜索找到了答案。我偶然发现了这个用于jQGrid的PHP CRUD模板。稍微调整一下,我就可以用我所有的网格工作了。一旦你开始更多地使用它,你会发现有很多改进和扩展的空间。这应该会让您朝着正确的方向开始。

查看此链接,谢谢Andrey,但这篇博文似乎关注的是商业产品jQGrid for PHP……我觉得很奇怪。。我一直在到处搜索,找不到任何关于如何用我的网格实现添加/编辑/删除功能的好例子。。。任何人都有他们知道的例子或教程吗?请考虑在答案中直接提供解决方案。实际上,它符合一个仅链接的答案,而这个答案不符合SO标准。@BartoszKP链接就是答案。为什么我要不必要地复制/粘贴链接的内容?这不是不必要的,因为链接可能会失效。目前,该政策已经确立。例如,请阅读,如果你真的感兴趣的话,也许还有很多关于这个的元讨论。不管是哪种方式,大家的共识是,只链接的答案是不受欢迎的。@BartoszKP那么,如果链接中有链接(如本例中所示),该怎么办?还是链接中的可下载文件?如果有一个文件上传功能,而不是期望我们在这里输入答案,那就太好了。我不知道。我只知道,你可以发布一个符合或不符合质量标准的答案。如果可以,请继续。如果你不能,你不应该——你可以发表评论。否则答案很可能会被删除。