<?php
	require_once('session.php');
    $users = $u['user'];
    $pid = $_POST['pid'];
    $angka = $_POST['angka'];
    $sql_21 = mysqli_query($conn,"SELECT * FROM `tb_pasaran` WHERE cuid = '$pid'") or die(mysqli_error());
    $s21 = mysqli_fetch_array($sql_21);
    $kodenya = $s21['code'];

    $getPeriode = mysqli_query($conn,"SELECT * FROM `tb_periode` WHERE pid = '$pid' ORDER BY no DESC LIMIT 1") or die(mysqli_error());
    $gppp = mysqli_num_rows($getPeriode);
    if($gppp == 0){
        $periode = 1;
    }
    else {
        $gpp = mysqli_fetch_array($getPeriode);
        $periode = $gpp['no']+1;
    }  

    $created_date = date('Y-m-d H:i:s');
    $created = date('Y-m-d');

    //Result 4D //
    $result4d = $angka;
    $insert4d1 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 1, '$periode', '$result4d', '4D', '$created_date')") or die(mysqli_error());
    $cekMenang1 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 1 AND `periode` = '$periode' AND `tebak` = '$result4d' AND `posisi` = '4D'") or die(mysqli_error());
    $cmm1 = mysqli_num_rows($cekMenang1);
    if($cmm1 > 0){
        while($cm1 = mysqli_fetch_array($cekMenang1)){
            $cuid = $cm1['cuid'];
            $pemenang = $cm1['userID'];
            $jumlah = $cm1['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result 3D //
    $result3d = substr($angka,-3);
    $insert4d2 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 1, '$periode', '$result3d', '3D', '$created_date')") or die(mysqli_error());
    $cekMenang2 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `periode` = '$periode' AND `tebak` = '$result3d' AND `posisi` = '3D'") or die(mysqli_error());
    $cmm2 = mysqli_num_rows($cekMenang2);
    if($cmm2 > 0){
        while($cm1 = mysqli_fetch_array($cekMenang2)){
            $cuid = $cm2['cuid'];
            $pemenang = $cm2['userID'];
            $jumlah = $cm2['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result 2D //
    $result2d = substr($angka,-2);
    $insert4d3 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 1, '$periode', '$result2d', '2D', '$created_date')") or die(mysqli_error());
    $cekMenang3 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 1 AND `periode` = '$periode' AND `tebak` = '$result2d' AND `posisi` = '2D'") or die(mysqli_error());
    $cmm3 = mysqli_num_rows($cekMenang3);
    if($cmm3 > 0){
        while($cm3 = mysqli_fetch_array($cekMenang3)){
            $cuid = $cm3['cuid'];
            $pemenang = $cm3['userID'];
            $jumlah = $cm3['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result COLOK BEBAS //
    $result1 = substr($angka,0,1);
    $result2 = substr($angka,1,1);
    $result3 = substr($angka,2,1);
    $result4 = substr($angka,3,1);
    $insert4d4 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 5, '$periode', '$result1', '', '$created_date')") or die(mysqli_error());
    $cekMenang4 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 5 AND `periode` = '$periode' AND `tebak` = '$result1'") or die(mysqli_error());
    $cmm4 = mysqli_num_rows($cekMenang4);
    if($cmm4 > 0){
        while($cm4 = mysqli_fetch_array($cekMenang4)){
            $cuid = $cm4['cuid'];
            $pemenang = $cm4['userID'];
            $jumlah = $cm4['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d5 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 5, '$periode', '$result2', '', '$created_date')") or die(mysqli_error());
    $cekMenang5 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 5 AND `periode` = '$periode' AND `tebak` = '$result2'") or die(mysqli_error());
    $cmm5 = mysqli_num_rows($cekMenang5);
    if($cmm5 > 0){
        while($cm5 = mysqli_fetch_array($cekMenang5)){
            $cuid = $cm5['cuid'];
            $pemenang = $cm5['userID'];
            $jumlah = $cm5['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d6 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 5, '$periode', '$result3', '', '$created_date')") or die(mysqli_error());
    $cekMenang6 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 5 AND `periode` = '$periode' AND `tebak` = '$result3'") or die(mysqli_error());
    $cmm6 = mysqli_num_rows($cekMenang6);
    if($cmm6 > 0){
        while($cm6 = mysqli_fetch_array($cekMenang6)){
            $cuid = $cm6['cuid'];
            $pemenang = $cm6['userID'];
            $jumlah = $cm6['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d7 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 5, '$periode', '$result4', '', '$created_date')") or die(mysqli_error());
    $cekMenang7 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 5 AND `periode` = '$periode' AND `tebak` = '$result4'") or die(mysqli_error());
    $cmm7 = mysqli_num_rows($cekMenang7);
    if($cmm7 > 0){
        while($cm7 = mysqli_fetch_array($cekMenang7)){
            $cuid = $cm7['cuid'];
            $pemenang = $cm7['userID'];
            $jumlah = $cm7['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result COLOK BEBAS 2D //
    $resultA = $result1.$result2;
    $resultB = $result1.$result3;
    $resultC = $result1.$result4;
    $resultD = $result2.$result1;
    $resultE = $result2.$result3;
    $resultF = $result2.$result4;
    $resultG = $result3.$result1;
    $resultH = $result3.$result2;
    $resultI = $result3.$result4;
    $resultJ = $result4.$result1;
    $resultK = $result4.$result2;
    $resultL = $result4.$result3;
    $insert4d8 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultA', '', '$created_date')") or die(mysqli_error());
    $cekMenang8 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultA'") or die(mysqli_error());
    $cmm8 = mysqli_num_rows($cekMenang8);
    if($cmm8 > 0){
        while($cm8 = mysqli_fetch_array($cekMenang8)){
            $cuid = $cm8['cuid'];
            $pemenang = $cm8['userID'];
            $jumlah = $cm8['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d9 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultB', '', '$created_date')") or die(mysqli_error());
    $cekMenang9 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultB'") or die(mysqli_error());
    $cmm9 = mysqli_num_rows($cekMenang9);
    if($cmm9 > 0){
        while($cm9 = mysqli_fetch_array($cekMenang9)){
            $cuid = $cm9['cuid'];
            $pemenang = $cm9['userID'];
            $jumlah = $cm9['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d10 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultC', '', '$created_date')") or die(mysqli_error());
    $cekMenang10 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultC'") or die(mysqli_error());
    $cmm10 = mysqli_num_rows($cekMenang10);
    if($cmm10 > 0){
        while($cm10 = mysqli_fetch_array($cekMenang10)){
            $cuid = $cm10['cuid'];
            $pemenang = $cm10['userID'];
            $jumlah = $cm10['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d11 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultD', '', '$created_date')") or die(mysqli_error());
    $cekMenang11 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultD'") or die(mysqli_error());
    $cmm11 = mysqli_num_rows($cekMenang11);
    if($cmm11 > 0){
        while($cm11 = mysqli_fetch_array($cekMenang11)){
            $cuid = $cm11['cuid'];
            $pemenang = $cm11['userID'];
            $jumlah = $cm11['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d12 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultE', '', '$created_date')") or die(mysqli_error());
    $cekMenang12 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultE'") or die(mysqli_error());
    $cmm12 = mysqli_num_rows($cekMenang12);
    if($cmm12 > 0){
        while($cm12 = mysqli_fetch_array($cekMenang12)){
            $cuid = $cm12['cuid'];
            $pemenang = $cm12['userID'];
            $jumlah = $cm12['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d13 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultF', '', '$created_date')") or die(mysqli_error());
    $cekMenang13 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultF'") or die(mysqli_error());
    $cmm13 = mysqli_num_rows($cekMenang13);
    if($cmm13 > 0){
        while($cm13 = mysqli_fetch_array($cekMenang13)){
            $cuid = $cm13['cuid'];
            $pemenang = $cm13['userID'];
            $jumlah = $cm13['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d14 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultG', '', '$created_date')") or die(mysqli_error());
    $cekMenang14 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultG'") or die(mysqli_error());
    $cmm14 = mysqli_num_rows($cekMenang14);
    if($cmm14 > 0){
        while($cm14 = mysqli_fetch_array($cekMenang14)){
            $cuid = $cm14['cuid'];
            $pemenang = $cm14['userID'];
            $jumlah = $cm14['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d15 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultH', '', '$created_date')") or die(mysqli_error());
    $cekMenang15 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultH'") or die(mysqli_error());
    $cmm15 = mysqli_num_rows($cekMenang15);
    if($cmm15 > 0){
        while($cm15 = mysqli_fetch_array($cekMenang15)){
            $cuid = $cm15['cuid'];
            $pemenang = $cm15['userID'];
            $jumlah = $cm15['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d16 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultI', '', '$created_date')") or die(mysqli_error());
    $cekMenang16 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultI'") or die(mysqli_error());
    $cmm16 = mysqli_num_rows($cekMenang16);
    if($cmm16 > 0){
        while($cm16 = mysqli_fetch_array($cekMenang16)){
            $cuid = $cm16['cuid'];
            $pemenang = $cm16['userID'];
            $jumlah = $cm16['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d17 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultJ', '', '$created_date')") or die(mysqli_error());
    $cekMenang17 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultJ'") or die(mysqli_error());
    $cmm17 = mysqli_num_rows($cekMenang17);
    if($cmm17 > 0){
        while($cm17 = mysqli_fetch_array($cekMenang17)){
            $cuid = $cm17['cuid'];
            $pemenang = $cm17['userID'];
            $jumlah = $cm17['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d18 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultK', '', '$created_date')") or die(mysqli_error());
    $cekMenang18 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultK'") or die(mysqli_error());
    $cmm18 = mysqli_num_rows($cekMenang18);
    if($cmm18 > 0){
        while($cm18 = mysqli_fetch_array($cekMenang18)){
            $cuid = $cm18['cuid'];
            $pemenang = $cm18['userID'];
            $jumlah = $cm18['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d19 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 6, '$periode', '$resultL', '', '$created_date')") or die(mysqli_error());
    $cekMenang19 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 6 AND `periode` = '$periode' AND `tebak` = '$resultL'") or die(mysqli_error());
    $cmm19 = mysqli_num_rows($cekMenang19);
    if($cmm19 > 0){
        while($cm19 = mysqli_fetch_array($cekMenang19)){
            $cuid = $cm19['cuid'];
            $pemenang = $cm19['userID'];
            $jumlah = $cm19['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result COLOK JITU //
    $resultAs = substr($angka,0,1);
    $resultKop = substr($angka,1,1);
    $resultKepala = substr($angka,2,1);
    $resultEkor = substr($angka,3,1);
    $insert4d20 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 8, '$periode', '$resultAs', 'AS', '$created_date')") or die(mysqli_error());
    $cekMenang20 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 8 AND `periode` = '$periode' AND `tebak` = '$resultAs' AND `posisi` = 'AS'") or die(mysqli_error());
    $cmm20 = mysqli_num_rows($cekMenang20);
    if($cmm20 > 0){
        while($cm20 = mysqli_fetch_array($cekMenang20)){
            $cuid = $cm20['cuid'];
            $pemenang = $cm20['userID'];
            $jumlah = $cm20['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d21 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 8, '$periode', '$resultKop', 'KOP', '$created_date')") or die(mysqli_error());
    $cekMenang21 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 8 AND `periode` = '$periode' AND `tebak` = '$resultKop' AND `posisi` = 'KOP'") or die(mysqli_error());
    $cmm21 = mysqli_num_rows($cekMenang21);
    if($cmm21 > 0){
        while($cm21 = mysqli_fetch_array($cekMenang21)){
            $cuid = $cm21['cuid'];
            $pemenang = $cm21['userID'];
            $jumlah = $cm21['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d22 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 8, '$periode', '$resultKepala', 'KEPALA', '$created_date')") or die(mysqli_error());
    $cekMenang22 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 8 AND `periode` = '$periode' AND `tebak` = '$resultKepala' AND `posisi` = 'KEPALA'") or die(mysqli_error());
    $cmm22 = mysqli_num_rows($cekMenang22);
    if($cmm22 > 0){
        while($cm22 = mysqli_fetch_array($cekMenang22)){
            $cuid = $cm22['cuid'];
            $pemenang = $cm22['userID'];
            $jumlah = $cm22['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d23 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 8, '$periode', '$resultEkor', 'EKOR', '$created_date')") or die(mysqli_error());
    $cekMenang23 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 8 AND `periode` = '$periode' AND `tebak` = '$resultEkor' AND `posisi` = 'EKOR'") or die(mysqli_error());
    $cmm23 = mysqli_num_rows($cekMenang23);
    if($cmm23 > 0){
        while($cm23 = mysqli_fetch_array($cekMenang23)){
            $cuid = $cm23['cuid'];
            $pemenang = $cm23['userID'];
            $jumlah = $cm23['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result TENGAH //
    $e = substr($angka,2,2);
    if($e >= 25 && $e <= 74){
        $resultTengah = 'TENGAH';
    }
    else {
        $resultTengah = 'TEPI';
    }
    $insert4d24 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 9, '$periode', '$resultTengah', '', '$created_date')") or die(mysqli_error());
    $cekMenang24 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 9 AND `periode` = '$periode' AND `tebak` = '$resultTengah'") or die(mysqli_error());
    $cmm24 = mysqli_num_rows($cekMenang24);
    if($cmm24 > 0){
        while($cm24 = mysqli_fetch_array($cekMenang24)){
            $cuid = $cm24['cuid'];
            $pemenang = $cm24['userID'];
            $jumlah = $cm24['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result DASAR //
    $c = substr($angka,2,1);
    $d = substr($angka,3,1);
    $e = $c + $d;
    if($e > 9){
        $f = substr($e,0,1);
        $g = substr($e,1,1);
        $apa = $f + $g;
    }
    else {
        $apa = $e;
    }
    if($apa%2){
        $dasar1 = 'GANJIL';
    }
    else {
        $dasar1 = 'GENAP';
    }
    if($apa < 5){
        $dasar2 = 'KECIL';
    }
    else {
        $dasar2 = 'BESAR';
    }
    $insert4d25 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 13, '$periode', '$dasar1', '', '$created_date')") or die(mysqli_error());
    $cekMenang25 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 13 AND `periode` = '$periode' AND `tebak` = '$dasar1'") or die(mysqli_error());
    $cmm25 = mysqli_num_rows($cekMenang25);
    if($cmm25 > 0){
        while($cm25 = mysqli_fetch_array($cekMenang25)){
            $cuid = $cm25['cuid'];
            $pemenang = $cm25['userID'];
            $jumlah = $cm25['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }
    $insert4d26 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 13, '$periode', '$dasar2', '', '$created_date')") or die(mysqli_error());
    $cekMenang26 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 13 AND `periode` = '$periode' AND `tebak` = '$dasar2'") or die(mysqli_error());
    $cmm26 = mysqli_num_rows($cekMenang26);
    if($cmm26 > 0){
        while($cm26 = mysqli_fetch_array($cekMenang26)){
            $cuid = $cm26['cuid'];
            $pemenang = $cm26['userID'];
            $jumlah = $cm26['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result SHIO //
    $a = substr($angka,2,1);
    $b = substr($angka,3,1);
    $aa = $a.$b;
    if($aa == '00'){
        $aaa = 100;
    }
    else {
        $aaa = $aa;
    }

    if((int)$aaa <= 12){
        $g = $aaa;
    }
    else {
        $h = ($aa / 12);
        $h = floor($h);
        $g = $aa - (12*$h);
        if($g == 0){
            $gg = 12;
        }
        else {
            $gg = $g;
        }
    }
    if($gg == 1){
        $posisi = 'Kuda';
    }
    else if ($gg == 2){
        $posisi = 'Ular';
    }
    else if ($gg == 3){
        $posisi = 'Naga';        
    }
    else if ($gg == 4){
        $posisi = 'Kelinci';        
    }
    else if ($gg == 5){
        $posisi = 'Harimau';        
    }
    else if ($gg == 6){
        $posisi = 'Kerbau';        
    }
    else if ($gg == 7){
        $posisi = 'Tikus';        
    }
    else if ($gg == 8){
        $posisi = 'Babi';        
    }
    else if ($gg == 9){
        $posisi = 'Anjing';        
    }
    else if ($gg == 10){
        $posisi = 'Ayam';        
    }
    else if ($gg == 11){
        $posisi = 'Monyet';        
    }
    else if ($gg == 12){
        $posisi = 'Kambing';        
    }
    $insert4d27 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 22, '$periode', '$gg', '$posisi', '$created_date')") or die(mysqli_error());
    $cekMenang27 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 22 AND `periode` = '$periode' AND `tebak` = '$gg' AND `posisi` = '$posisi'") or die(mysqli_error($conn));
    $cmm27 = mysqli_num_rows($cekMenang27);
    if($cmm27 > 0){
        while($cm27 = mysqli_fetch_array($cekMenang27)){
            $cuid = $cm27['cuid'];
            $pemenang = $cm27['userID'];
            $jumlah = $cm27['jumlah'];
            $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
            $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

        }
    }

    //Result SILANG - HOMO //
    if($result1%2){
        $bb = 'GANJIL';
    }
    else {
        $bb = 'GENAP';
    }
    if($result2%2){
        $cc = 'GANJIL';
    }
    else {
        $cc = 'GENAP';
    }
    if($result3%2){
        $dd = 'GANJIL';
    }
    else {
        $dd = 'GENAP';
    }
    if($result4%2){
        $ee = 'GANJIL';
    }
    else {
        $ee = 'GENAP';
    }

    if($bb == $cc){
        $resultDepan = 'HOMO';
        $insert4d28 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 23, '$periode', '$resultDepan', 'DEPAN', '$created_date')") or die(mysqli_error());
        $cekMenang28 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 23 AND `periode` = '$periode' AND `tebak` = '$resultDepan' AND `posisi` = 'DEPAN'") or die(mysqli_error());
        $cmm28 = mysqli_num_rows($cekMenang28);
        if($cmm28 > 0){
            while($cm28 = mysqli_fetch_array($cekMenang28)){
                $cuid = $cm28['cuid'];
                $pemenang = $cm28['userID'];
                $jumlah = $cm28['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else {
        $resultDepan = 'SILANG';
        $insert4d28 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 23, '$periode', '$resultDepan', 'DEPAN', '$created_date')") or die(mysqli_error());
        $cekMenang28 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 23 AND `periode` = '$periode' AND `tebak` = '$resultDepan' AND `posisi` = 'DEPAN'") or die(mysqli_error());
        $cmm28 = mysqli_num_rows($cekMenang28);
        if($cmm28 > 0){
            while($cm28 = mysqli_fetch_array($cekMenang28)){
                $cuid = $cm28['cuid'];
                $pemenang = $cm28['userID'];
                $jumlah = $cm28['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }

    if($cc == $dd){
        $resultTengah = 'HOMO';
        $insert4d29 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 23, '$periode', '$resultTengah', 'TENGAH', '$created_date')") or die(mysqli_error());
        $cekMenang29 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 23 AND `periode` = '$periode' AND `tebak` = '$resultTengah' AND `posisi` = 'TENGAH'") or die(mysqli_error());
        $cmm29 = mysqli_num_rows($cekMenang29);
        if($cmm29 > 0){
            while($cm29 = mysqli_fetch_array($cekMenang29)){
                $cuid = $cm29['cuid'];
                $pemenang = $cm29['userID'];
                $jumlah = $cm29['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else {
        $resultTengah = 'SILANG';
        $insert4d29 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 23, '$periode', '$resultTengah', 'TENGAH', '$created_date')") or die(mysqli_error());
        $cekMenang29 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 23 AND `periode` = '$periode' AND `tebak` = '$resultTengah' AND `posisi` = 'TENGAH'") or die(mysqli_error());
        $cmm29 = mysqli_num_rows($cekMenang29);
        if($cmm29 > 0){
            while($cm29 = mysqli_fetch_array($cekMenang29)){
                $cuid = $cm29['cuid'];
                $pemenang = $cm29['userID'];
                $jumlah = $cm29['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }

    if($dd == $ee){
        $resultBelakang = 'HOMO';
        $insert4d30 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 23, '$periode', '$resultBelakang', 'BELAKANG', '$created_date')") or die(mysqli_error());
        $cekMenang30 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 23 AND `periode` = '$periode' AND `tebak` = '$resultBelakang' AND `posisi` = 'BELAKANG'") or die(mysqli_error());
        $cmm30 = mysqli_num_rows($cekMenang30);
        if($cmm30 > 0){
            while($cm30 = mysqli_fetch_array($cekMenang30)){
                $cuid = $cm30['cuid'];
                $pemenang = $cm30['userID'];
                $jumlah = $cm30['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else {
        $resultBelakang = 'SILANG';
        $insert4d30 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 23, '$periode', '$resultBelakang', 'BELAKANG', '$created_date')") or die(mysqli_error());
        $cekMenang30 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 23 AND `periode` = '$periode' AND `tebak` = '$resultBelakang' AND `posisi` = 'BELAKANG'") or die(mysqli_error());
        $cmm30 = mysqli_num_rows($cekMenang30);
        if($cmm30 > 0){
            while($cm30 = mysqli_fetch_array($cekMenang30)){
                $cuid = $cm30['cuid'];
                $pemenang = $cm30['userID'];
                $jumlah = $cm30['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    

    //Result KEMBANG KEMPIS KEMBAR //
    if($result1 == $result2){
        $resulteDepan = 'KEMBAR';
        $insert4d31 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteDepan', 'DEPAN', '$created_date')") or die(mysqli_error());
        $cekMenang31 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteDepan' AND `posisi` = 'DEPAN'") or die(mysqli_error());
        $cmm31 = mysqli_num_rows($cekMenang31);
        if($cmm31 > 0){
            while($cm31 = mysqli_fetch_array($cekMenang31)){
                $cuid = $cm31['cuid'];
                $pemenang = $cm31['userID'];
                $jumlah = $cm31['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else if($result1 < $result2){
        $resulteDepan = 'KEMBANG';
        $insert4d31 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteDepan', 'DEPAN', '$created_date')") or die(mysqli_error());
        $cekMenang31 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteDepan' AND `posisi` = 'DEPAN'") or die(mysqli_error());
        $cmm31 = mysqli_num_rows($cekMenang31);
        if($cmm31 > 0){
            while($cm31 = mysqli_fetch_array($cekMenang31)){
                $cuid = $cm31['cuid'];
                $pemenang = $cm31['userID'];
                $jumlah = $cm31['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else {
        $resulteDepan = 'KEMPIS';
        $insert4d31 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteDepan', 'DEPAN', '$created_date')") or die(mysqli_error());
        $cekMenang31 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteDepan' AND `posisi` = 'DEPAN'") or die(mysqli_error());
        $cmm31 = mysqli_num_rows($cekMenang31);
        if($cmm31 > 0){
            while($cm31 = mysqli_fetch_array($cekMenang31)){
                $cuid = $cm31['cuid'];
                $pemenang = $cm31['userID'];
                $jumlah = $cm31['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }

    if($result2 == $result3){
        $resulteTengah = 'KEMBAR';
        $insert4d32 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteTengah', 'TENGAH', '$created_date')") or die(mysqli_error());
        $cekMenang32 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteTengah' AND `posisi` = 'TENGAH'") or die(mysqli_error());
        $cmm32 = mysqli_num_rows($cekMenang32);
        if($cmm32 > 0){
            while($cm32 = mysqli_fetch_array($cekMenang32)){
                $cuid = $cm32['cuid'];
                $pemenang = $cm32['userID'];
                $jumlah = $cm32['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else if($result2 < $result3){
        $resulteTengah = 'KEMBANG';
        $insert4d32 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteTengah', 'TENGAH', '$created_date')") or die(mysqli_error());
        $cekMenang32 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteTengah' AND `posisi` = 'TENGAH'") or die(mysqli_error());
        $cmm32 = mysqli_num_rows($cekMenang32);
        if($cmm32 > 0){
            while($cm32 = mysqli_fetch_array($cekMenang32)){
                $cuid = $cm32['cuid'];
                $pemenang = $cm32['userID'];
                $jumlah = $cm32['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else {
        $resulteTengah = 'KEMPIS';
        $insert4d32 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteTengah', 'TENGAH', '$created_date')") or die(mysqli_error());
        $cekMenang32 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteTengah' AND `posisi` = 'TENGAH'") or die(mysqli_error());
        $cmm32 = mysqli_num_rows($cekMenang32);
        if($cmm32 > 0){
            while($cm32 = mysqli_fetch_array($cekMenang32)){
                $cuid = $cm32['cuid'];
                $pemenang = $cm32['userID'];
                $jumlah = $cm32['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }

    if($result3 == $result4){
        $resulteBelakang = 'KEMBAR';
        $insert4d33 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteBelakang', 'BELAKANG', '$created_date')") or die(mysqli_error());
        $cekMenang33 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteBelakang' AND `posisi` = 'BELAKANG'") or die(mysqli_error());
        $cmm33 = mysqli_num_rows($cekMenang33);
        if($cmm33 > 0){
            while($cm33 = mysqli_fetch_array($cekMenang33)){
                $cuid = $cm33['cuid'];
                $pemenang = $cm33['userID'];
                $jumlah = $cm33['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else if($result3 < $result4){
        $resulteBelakang = 'KEMBANG';
        $insert4d33 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteBelakang', 'BELAKANG', '$created_date')") or die(mysqli_error());
        $cekMenang33 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteBelakang' AND `posisi` = 'BELAKANG'") or die(mysqli_error());
        $cmm33 = mysqli_num_rows($cekMenang33);
        if($cmm33 > 0){
            while($cm33 = mysqli_fetch_array($cekMenang33)){
                $cuid = $cm33['cuid'];
                $pemenang = $cm33['userID'];
                $jumlah = $cm33['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    else {
        $resulteBelakang = 'KEMPIS';
        $insert4d33 = mysqli_query($conn,"INSERT INTO `tb_pasaran_result` (`pid`, `gameid`, `periode`, `p_result`, `posisi`, `created_date`) VALUES ('$pid', 30, '$periode', '$resulteBelakang', 'BELAKANG', '$created_date')") or die(mysqli_error());
        $cekMenang33 = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `pid` = '$pid' AND `gameid` = 30 AND `periode` = '$periode' AND `tebak` = '$resulteBelakang' AND `posisi` = 'BELAKANG'") or die(mysqli_error());
        $cmm33 = mysqli_num_rows($cekMenang33);
        if($cmm33 > 0){
            while($cm33 = mysqli_fetch_array($cekMenang33)){
                $cuid = $cm33['cuid'];
                $pemenang = $cm33['userID'];
                $jumlah = $cm33['jumlah'];
                $setWin = mysqli_query($conn,"UPDATE `tb_taruhan` SET `keterangan` = 'Menang' WHERE cuid = '$cuid'") or die(mysqli_error());
                $setBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$jumlah' WHERE userID = '$pemenang'") or die(mysqli_error());

            }
        }
    }
    
    $updateTaruhan = mysqli_query($conn,"UPDATE `tb_taruhan` SET status = 1 WHERE `pid` = '$pid' AND `periode` = '$periode'") or die(mysqli_error());
    $insertPeriode = mysqli_query($conn,"INSERT INTO `tb_periode` (`pid`, `no`, `code`, `created`, `result`, `created_date`, `status`) VALUES ('$pid','$periode','$kodenya','$created', '$angka', '$created_date', 1)") or die(mysqli_error());
    header('location:../result/');
    exit();
    
?>