简体   繁体   中英

Editablegrid mysql binding not updating

I am using editablegrid with mysql. It binds and display data to my grid. However when I try to edit or update the grid it fails to do so.

Below is the some of the code of my loaddata,

$grid->addColumn('CertificateNo', 'CertificateNo', 'integer', NULL, false); 
$grid->addColumn('ID', 'ID', 'integer');
$grid->addColumn('QuizNo', 'Test No', 'integer');  
$grid->addColumn('Received', 'Received', 'boolean');  
$grid->addColumn('DateReceived', 'Date Received', 'datetime'); 

Below is code for the update script:

    // Get all parameters provided by the javascript
$colname = $mysqli->real_escape_string(strip_tags($_POST['colname']));
$id = $mysqli->real_escape_string(strip_tags($_POST['id']));
$coltype = $mysqli->real_escape_string(strip_tags($_POST['coltype']));
$value = $mysqli->real_escape_string(strip_tags($_POST['newvalue']));
$tablename = $mysqli->real_escape_string(strip_tags($_POST['tablename']));
    / This very generic. So this script can be used to update several tables.
$return=false;
if ( $stmt = $mysqli->prepare("UPDATE ".$tablename." SET ".$colname." = ? WHERE id = ?")) {
    $stmt->bind_param("si",$value, $id);
    $return = $stmt->execute();
    $stmt->close();

Below is the part of javascript which passes the value to my update.php script.

    function updateCellValue(editableGrid, rowIndex, columnIndex, oldValue, newValue, row, onResponse)
{      
    $.ajax({
        url: 'update.php',
        type: 'POST',
        dataType: "html",
        data: {
            tablename : editableGrid.name,
            id: editableGrid.getRowId(rowIndex), 
            newvalue: editableGrid.getColumnType(columnIndex) == "boolean" ? (newValue ? 1 : 0) : newValue, 
            colname: editableGrid.getColumnName(columnIndex),
            coltype: editableGrid.getColumnType(columnIndex)            
        },
        success: function (response) 
        { 
            // reset old value if failed then highlight row
            var success = onResponse ? onResponse(response) : (response == "ok" || !isNaN(parseInt(response))); // by default, a sucessfull reponse can be "ok" or a database id 
            if (!success) editableGrid.setValueAt(rowIndex, columnIndex, oldValue);
            highlight(row.id, success ? "ok" : "error"); 
        },
        error: function(XMLHttpRequest, textStatus, exception) { alert("Ajax failure\n" + errortext); },
        async: true
    });

This template came with a javascript editablegrid-2.0.1. I noticed the problem has to do with primary key. In the demo which can be found from www.editablegrid.net the table has primary key ID whereas mine has CertificateNo but ID in my table is not a primary key.

So I changed the

$grid->addColumn('ID', 'ID', 'integer', NULL, false);

to

$grid->addColumn('CertificateNo', 'CertificateNo', 'integer', NULL, false); 

Now I can not update the grid.

I had the same problem, you could see this code in loaddata.php

$grid->addColumn('action', 'Action', 'html', NULL, false, 'id'); 

replace 'id' at the last column with 'CertificateNo' and it will be like this

$grid->addColumn('action', 'Action', 'html', NULL, false, 'CertificateNo');

And then reload your page to see the changes.

update.php使用$ _POST ['id'],也许您应该将其更改为$ _POST ['CertificateNo']并将该ID作为另一列进行处理,因为它不再是主键。

I had the same problem. You have to change in js/demo.js in

function DatabaseGrid() 
{ 
    this.editableGrid = new EditableGrid("HERE!!! Chage to name of your sql table", {...

and also rename your CertificateNo to "id" or make new before it, because the grid works with it on many places, so it's hard to change everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM