<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
 
foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $co_id = $paramValue; 
  else if ($paramName == "surname") 
    $surname = $paramValue; 
  else if ($paramName == "firstname") 
    $firstname = $paramValue; 
  else if ($paramName == "middlename") 
    $middlename = $paramValue; 
  else if ($paramName == "title") 
    $title = $paramValue; 
}

$dal = new DAL();

if ($co_id == 0) {
  $co_id = hexdec(uniqid());

  $sql = "INSERT INTO contact " .
         "(id, surname, firstname, middlename, title) " .
         "VALUES " .
         "($co_id, '$surname', '$firstname', '$middlename', '$title') ";
  $result = $dal->query($sql);
}  

else {

  $sql = "UPDATE contact SET " .
         "surname='$surname', firstname='$firstname', " .
         "middlename='$middlename', title='$title' " .
         "WHERE id = $co_id ";
  $result = $dal->query($sql);

}  

echo json_encode($co_id);

?>

