AJAX 更新到多个数据库表

AJAX update to multiple database tables

本文关键字:数据库 更新 AJAX      更新时间:2023-09-26

我有一个从两个mysql表中读取数据的ajax表,这些表是

project_ownerships - id_own、项目、代码、own_current own_future
项目 - ID, 项目, 位置, 类型, 类型2, 区域, 运输, 阶段

数据使用联接表 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 值并更新错误的数据库记录

这是更新函数。此函数返回错误"找不到具有索引或名称 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();
}

基本上我想做的....

如果从 SQL 中删除projects.id则更新将不起作用

如果将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列或行索引列?

我正在研究的理论是

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 问题

应使用 AS 运算符重命名 SQL 查询中每个表的"id"列,以便它们不会冲突。

例如:

SELECT B.*,A.name,A.id as author_id FROM BOOKS B
INNER JOIN AUTHORS A ON A.id=B.authorid