Add and update entries!

This commit is contained in:
Przemek Grondek 2014-08-16 18:37:42 +02:00
parent d8441d5924
commit 282aad8d6f

72
c.php
View File

@ -93,6 +93,65 @@
$ret = mysql_fetch_object($result_id); $ret = mysql_fetch_object($result_id);
return $ret->total_count; return $ret->total_count;
} }
function updateElement($table, $headerJSON, $oldValuesJSON, $newValuesJSON){
$header = json_decode($headerJSON);
$oldValues = json_decode($oldValuesJSON);
$newValues = json_decode($newValuesJSON);
$query = "UPDATE `$table` SET ";
for($i = 0;$i < sizeof($header); $i++){
$query .= "`".$header[$i]."` = '".$newValues[$i]."' ";
if($i+1 < sizeof($header))
$query .= ", ";
}
$query .= " WHERE ( ";
for($i = 0;$i < sizeof($header); $i++){
$query .= "`".$header[$i]."` = '".$oldValues[$i]."' ";
if($i+1 < sizeof($header))
$query .= " && ";
}
$query .= ");";
$result_id = mysql_query($query);
if( !$result_id ){
echo mysql_error();
}
$info = mysql_info();
echo $info ;
}
function addElement($table, $headerJSON, $valuesJSON){
$header = json_decode($headerJSON);
$values = json_decode($valuesJSON);
$query = "INSERT INTO `$table` (";
for($i = 0;$i < sizeof($header); $i++){
$query .= "`".$header[$i]."`";
if($i+1 < sizeof($header))
$query .= ", ";
}
$query .= ") VALUES ( ";
for($i = 0;$i < sizeof($header); $i++){
$query .= "'".$values[$i]."' ";
if($i+1 < sizeof($header))
$query .= " , ";
}
$query .= ");";
$result_id = mysql_query($query);
if( !$result_id ){
echo mysql_error();
} else {
echo OK;
}
$info = mysql_info();
echo $info ;
}
} }
$action = $_GET['a']; $action = $_GET['a'];
@ -106,6 +165,9 @@
$start = $_GET['s']; $start = $_GET['s'];
$limit = $_GET['l']; $limit = $_GET['l'];
$query = $_GET['q']; $query = $_GET['q'];
$header = $_GET['h'];
$values = $_GET['v'];
$oldValues = $_GET['o'];
if(($user == null) || ($password == null) || ($action == null)) if(($user == null) || ($password == null) || ($action == null))
die('Nope'); die('Nope');
@ -144,6 +206,16 @@
$con->selectDb($database); $con->selectDb($database);
echo json_encode($con->selectTable($table, $start, $limit)); echo json_encode($con->selectTable($table, $start, $limit));
break; break;
case "updateelement":
$con->connect();
$con->selectDb($database);
$con->updateElement($table, $header, $oldValues, $values);
break;
case "addelement":
$con->connect();
$con->selectDb($database);
$con->addElement($table, $header, $values);
break;
} }
$con->disconnect(); $con->disconnect();