<?php
include"../../../../inc/conn.php";

// CEK HAK AKSES DISINI

$id_siswa			= str_replace("'","`",(isset($_POST['id_siswa'])) ? $_POST['id_siswa'] : "");
$nama_lengkap		= str_replace("'","`",(isset($_POST['nama_lengkap'])) ? $_POST['nama_lengkap'] : "");
$username			= str_replace("'","`",(isset($_POST['username'])) ? $_POST['username'] : "");

$qsiswa=mysqli_query($connect,"select * from tb_siswa where id_siswa='$id_siswa'");
$rsiswa=mysqli_fetch_array($qsiswa);
$kelas=$rsiswa['id_kelas'];

if($username==$rsiswa['username']){

	$update = mysqli_query($connect,"update tb_siswa set 

			nama_lengkap 		= '".$nama_lengkap."',
			username 			= '".$username."'

		where id_siswa='$id_siswa'")or die(mysqli_error($connect));


	if($update){
		echo"<script>alert('Data berhasil diperbarui !');</script>";
		echo"<script>document.location.href='index.php?prefix=datasiswa&kelas=$kelas'</script>";
	}else{
		echo"<script>alert('Gagal mengeksekusi perintah ini !');history.go(-1)</script>";
	}

}else{

	$ceksiswa=mysqli_query($connect,"select * from tb_siswa where username='$username'");
	$jumceksiswa=mysqli_num_rows($ceksiswa);
	
	if($jumceksiswa!=0){
		echo"<script>alert('Username tidak dapat digunakan !');history.go(-1)</script>";
	}else{

		$update = mysqli_query($connect,"update tb_siswa set 

			nama_lengkap 		= '".$nama_lengkap."',
			username 			= '".$username."'

			where id_siswa='$id_siswa'")or die(mysqli_error($connect));


		if($update){
			echo"<script>alert('Data berhasil diperbarui !');</script>";
			echo"<script>document.location.href='index.php?prefix=datasiswa&kelas=$kelas'</script>";
		}else{
			echo"<script>alert('Gagal mengeksekusi perintah ini !');history.go(-1)</script>";
		}

	}
}

?>