2012-07-05 63 views
4

我有一個ajax表從兩個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和更新在這裏錯了DB-記錄

的更新功能。該函數將返回錯誤「不與索引或名稱中列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.idprojects.id被刪除,更新將工作,但只有project_ownership.*領域

所以......我需要在一個名爲*.id

單獨的SQL查詢,當更新發送一列,可以選擇正確的表名通過

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是療法e,然後將參數傳遞給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"))

UPDATE

有一個在editablegrid定義行ID是什麼一對夫婦的私有函數。所以我想這使得它成爲一個editablegrid的問題,而不是一個真正的javascript,ajax或php問題適合這裏

+3

你在你粘貼代碼的第一位的注入漏洞。您應該轉義您在查詢字符串中使用的值。或者使用PDO或mysql_real_escape_string – Eva 2012-07-25 00:52:23

+0

就故障排除而言,只是警告「editablegrid」的值或其他值,以確保它傳遞正確的值 – Eva 2012-07-25 00:55:29

+0

...您還可以將整個editableGrid對象傳遞給一個php頁面,然後執行print_r在$ _POST上以更好地瞭解結構 – Eva 2012-07-25 00:56:34

回答

1

你應該重命名你的SQL查詢中的每個表的「id」列與AS運營商,所以他們不衝突。

例如:

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