【问题标题】:AJAX update to multiple database tablesAJAX 更新多个数据库表
【发布时间】:2012-07-05 12:15:45
【问题描述】:

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

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);

但是我无法让编辑更新正常工作。我只能从一个 db 表中获取 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 问题

【问题讨论】:

  • 您粘贴的第一段代码中存在注入漏洞。您应该转义您在查询字符串中使用的值。使用 PDO 或 mysql_real_escape_string
  • 就故障排除而言,只需提醒“editablegrid”的值或其他任何内容,以确保它传递了正确的值
  • ...您还可以将整个 editableGrid 对象传递给 php 页面,然后在 $_POST 上执行 print_r 以更好地了解结构

标签: javascript php jquery ajax editablegrid


【解决方案1】:

您应该使用 AS 运算符重命名 SQL 查询中每个表的“id”列,以免它们发生冲突。

例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多