<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
 
foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "table")
    $table = $paramValue;
  elseif ($paramName == "id") 
    $id = $paramValue;  
}

$firstOf = true;
foreach ($_POST as $paramName => $paramValue) {
  if (!($paramName == "table" || $paramName == "id"))   {
    if ($firstOf) {
      if ($id == 0) {
        $colList = $paramName;
        $valList = "'$paramValue'";
      }
      else 
        $updList = "$paramName='$paramValue'";
      $firstOf = false;
    }
    else {
      if ($id == 0) {
        $colList .= ", $paramName";
        $valList .= ", '$paramValue'";
      }
      else 
        $updList .= ", $paramName='$paramValue'";
    }
  }
} 
 
if ($id == 0) 
  $sql = "INSERT INTO $table ($colList) VALUES ($valList) ";
else
  $sql = "UPDATE $table SET $updList WHERE id = $id ";

$dal = new DAL();
$result = $dal->query($sql);

echo json_encode($result);

?>

