<?php
 
include 'dbconfig.php';
 
$kode = $_POST['kode'];
 
$nama = $_POST['nama'];
 
$alamat= $_POST['alamat'];

$groupho= $_POST['groupho'];

$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 kode_cabang from TbCabang where kode_cabang='".$kode."' ";
$res2 = mysqli_query($conn, $sqlcheck);
$sqlcheck2=mysqli_num_rows($res2);
 
if($sqlcheck2==0)
{
$query = "insert into TbCabang (kode_cabang, nama, alamat,groupho) values('".$kode."','".$nama."','".$alamat."','".$groupho."')";
$response = mysqli_query($conn, $query) or die('Error query:  '.$query);
} 

if ($sqlcheck2>0){
 
$query = "update TbCabang set nama='".$nama."',alamat='".$alamat."',groupho=".$groupho." 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);
 
}
 
?>
