我正在使用editablegrid與MySQL。它綁定並顯示數據到我的網格。但是,當我嘗試編輯或更新網格時,它不能這樣做。Editablegrid mysql綁定不更新
下面是我的一些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');
以下是更新腳本代碼:
// 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();
下面是一個價值傳遞給我更新的JavaScript的一部分.php腳本。
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
});
該模板附帶了javascript editablegrid-2.0.1。我注意到這個問題與主鍵有關。在可從www.editablegrid.net找到的演示中,該表具有主鍵ID,而我的證書號不是,但表中的ID不是主鍵。
所以我改變了
$grid->addColumn('ID', 'ID', 'integer', NULL, false);
到
$grid->addColumn('CertificateNo', 'CertificateNo', 'integer', NULL, false);
現在我不能更新的網格。