AJAX update for multiple database tables

I have an ajax table that reads data from two mysql tables, tables

project_ownerships - id_own, project, code, own_current, own_future
projects - id, project, location, type, type 2, region, transport, stage

data is read into the web page table in order using a sql table query connection.

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

however, I cannot get the editing update to work properly. I can only get the data - id value from one db table.

so that any updates related to the projects tag are perfectly updated, but any project_ownerships updates do not have the correct id value and update the wrong db record

here is the update function. this function returns the error "column not found with index or id_own name"

  $.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 }); 

here php update

 $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(); } 

basically what i am trying to do ....

if projects.id is removed from sql, the update will not work

If project_ownership.id_own changed to project_ownership.id and projects.id is removed, the update will work, but only with project_ownership.* fields project_ownership.*

so i need a column in sql query *.id

separately, when an update is sent, the correct table name can be selected using

 tablename: editableGrid.getColumnName(columnIndex) == "own_current" ? "project_ownerships" : "projects", 

therefore using the same logic

 id: editableGrid.getColumnName(columnIndex) == "own_current" ? editableGrid.getValueAt(rowIndex, editableGrid.getColumn("id_own")) : editableGrid.getRowId(rowIndex), 

first, it recognizes that own_current exists and passes the argument editableGrid.getValueAt(rowIndex, editableGrid.getColumn("id_own")) , but the returned error is "no column found with the name id_own " ...?

I'm really confused.

it cannot be (removed from sql), otherwise the update just freezes

but when he is there, he cannot be found ...

any help would be great

how can i define ajax - data - id column or rowIndex column?

I am working on a theory that

editableGrid.getRowId(rowIndex)

maybe something like (which obviously won't work otherwise)

editableGrid.getValueAt(rowIndex, editableGrid.getColumn("id_own"))

but I also tried, among others

editableGrid.getValueAt(rowIndex, editableGrid.getColumnIndex("id_own"))

which returns "invalid column index -1"

and

editableGrid.getValueAt(rowIndex, editableGrid.getColumnName("id_own"))

UPDATE

There are a couple of private functions in editablegrid that determine which row identifier. so I assume this is an editablegrid question and not quite suitable for javascript, ajax or php question

+4
source share
1 answer

You must rename the id column of each table in your SQL query using the AS statement so that they do not conflict.

For instance:

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

All Articles