我有一个从两个 mysql 表中读取数据的 ajax 表,这些表是
project_ownerships - id_own, project, code, own_current, own_future
projects - id, project, location, type, type2, area, transport, stage
使用连接表 sql 查询,数据可以很好地读入网页表。
$query_value = isset($_GET['value']) ? $_GET['value'] : "";
$sql = "SELECT project_ownerships.*, projects.*
FROM project_ownerships, projects
WHERE project_ownerships.project = projects.project
AND project_ownerships.code = $query_value'";
$result = $mysqli->query($sql);
但是我无法让编辑更新正常工作。我只能data - id
从一个数据库表中获取一个值。
所以属于projects
表的任何更新都可以更新,但任何更新project_ownerships
都没有正确的 id 值并更新错误的 db-record
这是更新功能。此函数返回错误“未找到索引或名称 id_own 的列”
$.ajax({
url: 'update_multi.php',
type: 'POST',
dataType: "html",
data: {
tablename: editableGrid.getColumnName(columnIndex) == "own_current" ? "project_ownerships" : "projects",
id: editableGrid.getColumnName(columnIndex) == "own_current" ? editableGrid.getValueAt(rowIndex, editableGrid.getColumn("id_own")) : editableGrid.getRowId(rowIndex),
newvalue: editableGrid.getColumnType(columnIndex) == "boolean" ? (newValue ? 1 : 0) : newValue,
colname: editableGrid.getColumnName(columnIndex),
coltype: editableGrid.getColumnType(columnIndex)
},
success: function (...,
error: function(...,
async: true
});
这是php更新
$tablename = $mysqli->real_escape_string(strip_tags($_POST['tablename']));
$id = $mysqli->real_escape_string(strip_tags($_POST['id']));
$value = $mysqli->real_escape_string($_POST['newvalue']);
$colname = $mysqli->real_escape_string(strip_tags($_POST['colname']));
$coltype = $mysqli->real_escape_string(strip_tags($_POST['coltype']));
$id_column = "";
if ($tablename == "projects") {$id_column = "id";} else {$id_column = "id_own";}
if ( $stmt = $mysqli->prepare("UPDATE ".$tablename." SET ".$colname." = ? WHERE ".$id_column." = ?")) {
$stmt->bind_param("si",$value, $id);
$return = $stmt->execute();
$stmt->close();
}
基本上我正在尝试做的事情......
如果projects.id
从 sql 中删除,则更新将不起作用
如果project_ownership.id_own
更改为project_ownership.id
并projects.id
删除,则更新将起作用,但仅适用于project_ownership.*
字段
所以...我需要在 sql 查询中有一列称为*.id
单独地,当发送更新时,可以通过以下方式选择正确的表名
tablename: editableGrid.getColumnName(columnIndex) == "own_current" ? "project_ownerships" : "projects",
所以使用相同的逻辑
id: editableGrid.getColumnName(columnIndex) == "own_current" ? editableGrid.getValueAt(rowIndex, editableGrid.getColumn("id_own")) : editableGrid.getRowId(rowIndex),
首先,认识到own_current
存在,并将参数传递给editableGrid.getValueAt(rowIndex, editableGrid.getColumn("id_own"))
但返回的错误是“找不到具有id_own
名称的列”......?
我真的很困惑。。
它不能不存在(从 sql 中删除)否则更新会冻结
但是当它在那里时找不到...
任何帮助都会很棒
如何定义ajax - data - id
列或 rowIndex 列?
我正在研究这个理论
editableGrid.getRowId(rowIndex)
可能是这样的(显然不是这样,否则这会起作用)
editableGrid.getValueAt(rowIndex, editableGrid.getColumn("id_own"))
但我也尝试过,除其他外
editableGrid.getValueAt(rowIndex, editableGrid.getColumnIndex("id_own"))
返回“无效的列索引-1”
和
editableGrid.getValueAt(rowIndex, editableGrid.getColumnName("id_own"))
更新
有几个私有函数editablegrid
可以定义行 id 是什么。所以我想这使它成为一个可编辑的网格问题,而不是真正适合这里的 javascript、ajax 或 php 问题