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

$kodecab = $_POST['kodecab'];
 
$hargajual= $_POST['hargajual'];

$hargabeli= $_POST['hargabeli'];
 
//$id = $_POST['id'];
 
// Create connection
 
$conn = mysqli_connect($servername, $username, $password, $dbname);
 
// Check connection
 
if (!$conn) {
 
    die("Connection failed: " . mysqli_connect_error());
 
}

/* delete data by kode */
 $querydel = "delete from tbl_barang where kode_cabang='".$kodecab."' and kode='".$kode."' " ;

$responsedel = mysqli_query($conn, $querydel) or die('Error query:  '.$querydel);

 
/* grab the posts from the db */
 
$query = "insert into tbl_barang (kode_cabang,kode, nama, harga,harga_beli) values('".$kodecab."','".$kode."','".$nama."','".$hargajual."','".$hargabeli."')";


/* 
if ($id != "0"){
 
$query = "update tbl_barang set nama='".$nama."',kode='".$kode."',harga='".$harga."',harga_beli='".$harga_beli."' where id=".$id;
 
}*/
 
$response = mysqli_query($conn, $query) or die('Error query:  '.$query);
 
$lid = mysqli_insert_id($conn);
 
$result["errormsg"]="Success";
 
$result["lid"]="$lid";
 
if ($response == 1){
 
    echo json_encode($result);
 
}
 
else{
 
    $result["errormsg"]="Fail";
 
    echo json_encode($result);
 
}
 
?>
