<?php
if(!defined('INDEX2')) die();

$foto   = $_FILES['foto']['name'];
$lokasi = $_FILES['foto']['tmp_name'];
$tipe   = $_FILES['foto']['type'];
$ukuran = $_FILES['foto']['size'];

$nama       = $_POST['nama_siswa'];
$nis         = $_POST['nis'];
$tgl        = $_POST['tanggal'];
$kelas      = $_POST['kelas'];
$jurusan        = $_POST['jurusan'];
$gender        = $_POST['gender'];

$error = "";

if($foto == ""){
    $query  = "INSERT INTO siswa SET ";
    $query .= "foto = '$foto', ";
    $query .= "nama_siswa = '$nama', ";
    $query .= "nis = '$nis', ";
    $query .= "tanggal_lahir = '$tgl', ";
    $query .= "kelas = '$kelas', ";
    $query .= "jurusan = '$jurusan', ";
    $query .= "gender = '$gender'";

    $result = mysqli_query($con,$query);
} else {
    if($tipe != "image/jpeg" and $tipe != "image/jpg" and $tipe != "image/png"){
        $error = "Maaf, tipe file tidak didukung!";
    } elseif($ukuran >= 1000000){
        echo $ukuran;
        $error = "Ukuran file terlalu besar (lebih dari 1 MB)!";
    } else {
        move_uploaded_file($lokasi,"images/".$foto);

    $query  = "INSERT INTO siswa SET ";
    $query .= "foto = '$foto', ";
    $query .= "nama_siswa = '$nama', ";
    $query .= "nis = '$nis', ";
    $query .= "tanggal_lahir = '$tgl', ";
    $query .= "kelas = '$kelas', ";
    $query .= "jurusan = '$jurusan', ";
    $query .= "gender = '$gender'";
    
        $result = mysqli_query($con,$query);
    }
}

if($error != ""){
    echo $error;
    echo "<meta http-equiv='refresh' content='2; url=?hal=siswa_tambah2'>";
} elseif ($query){
    echo "Berhasil menambahkan data pegawai <b>$nama</b>";
    echo "<meta http-equiv='refresh' content='2; url=?hal=siswa2'>";
} else {
    echo "Tidak dapat menyimpan data!<br>";
    echo mysqli_error();
}
?>