<?php
require("config/koneksi.php");
require("config/function.php");
$token = isset($_GET['token']) ? $_GET['token'] : 'false';
$querys = mysqli_query($koneksi, "select token_api from aplikasi where token_api='$token'");
$cektoken = mysqli_num_rows($querys);

if ($cektoken <> 0) {
    if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
        throw new Exception('Request method must be POST!');
    }

    $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
    if (strcasecmp($contentType, 'application/json') != 0) {
        throw new Exception('Content type must be: application/json');
    }

  
    $content = trim(file_get_contents("php://input"));
    $decoded = json_decode($content, true);

   
    if (!is_array($decoded)) {
        throw new Exception('Received content contained invalid JSON!');
    }

    foreach ($decoded as $nilai) {
        
                       mysqli_query($koneksi, "insert into nilai (id_ujian,id_bank,id_siswa,npsn,kode_ujian,ujian_mulai,ujian_berlangsung,ujian_selesai,jml_benar,benar_esai,benar_multi,benar_bs,benar_urut,skor,skor_esai,skor_multi,skor_bs,skor_urut,total,ipaddress,hasil,jawaban,jawaban_esai,jawaban_multi,jawaban_bs,jawaban_urut,id_soal,browser)
        values ('$nilai[id_ujian]','$nilai[id_bank]','$nilai[id_siswa]','$nilai[npsn]','$nilai[kode_ujian]','$nilai[ujian_mulai]','$nilai[ujian_berlangsung]','$nilai[ujian_selesai]',
		'$nilai[jml_benar]','$nilai[benar_esai]','$nilai[benar_multi]','$nilai[benar_bs]','$nilai[benar_urut]','$nilai[skor]','$nilai[skor_esai]','$nilai[skor_multi]','$nilai[skor_bs]','$nilai[skor_urut]','$nilai[total]','$nilai[ipaddress]','$nilai[hasil]','$nilai[jawaban]',
		'$nilai[jawaban_esai]','$nilai[jawaban_multi]','$nilai[jawaban_bs]','$nilai[jawaban_urut]','$nilai[id_soal]','$nilai[browser]')");

    }

    echo "berhasil";
} else {
    echo "<script>location.href='.'</script>";
}
