<?php
 
include 'dbconfig.php';
 
$user = $_POST['user'];
 
$pass = $_POST['pass'];
 
$hak= $_POST['hak'];

$idno = $_POST['idno'];
 
// Create connection
 
$conn = mysqli_connect($servername, $username, $password, $dbname);
 
// Check connection
 
if (!$conn) {
 
    die("Connection failed: " . mysqli_connect_error());
 
}
 
/* grab the posts from the db */

$sqlcheck="select user from TbUser where user='".$user."' ";
$res2 = mysqli_query($conn, $sqlcheck);
$sqlcheck2=mysqli_num_rows($res2);
 
if($sqlcheck2==0)
{
$query = "insert into TbUser (user, password, hak) values('".$user."','".$pass."','".$hak."')";
$response = mysqli_query($conn, $query) or die('Error query:  '.$query);
} 

if ($sqlcheck2>0){
 
$query = "update TbUser set user='".$user."',password='".$pass."',hak='".$hak."' where idno=".$idno;
$response = mysqli_query($conn, $query) or die('Error query:  '.$query); 
}
 

 
$lid = mysqli_insert_id($conn);
 
$result["errormsg"]="Success";
 
$result["lid"]="$lid";
 
if ($sqlcheck2==0 and $idno==0){
    
     //input data

    echo json_encode($result);
 
}

if ($sqlcheck2>0 and $idno>0){
    
     //edit data

    echo json_encode($result);
 
}

 
else{

     $result["errormsg"]="failed";
    echo json_encode($result);
 
}
 
?>
