<?php
include('includes/config.php');

$value=$_POST['value'];
$over=$_POST['over'];
$ball=$_POST['ball'];

     $sql = "SELECT * from `cricket_result` where `over`='$over' and `ball`='$ball'";
	 $tbl_result = mysqli_query($com, $sql); 
     $rowcount122 = mysqli_num_rows($tbl_result);
     if($rowcount122 == 0)
     {
         $bet_sql = "SELECT * from `cricket_bet` where `over`='$over' and `ball`='$ball'";
          $tbl_query = mysqli_query($com, $bet_sql); 
           $tbl_querycount = mysqli_num_rows($tbl_query);
        while($bet_rows = mysqli_fetch_array($tbl_query))
          {
            $name = $bet_rows['name'];
	        $userid = $bet_rows['userid'];
         	$bet_amount = $bet_rows['pay_amount'];
            	if($name == $value)
	             {
	    
                 $wallet_sql = mysqli_query($com,"select * from `wallet` where `userid` = '$userid'");
	        	$wallet_rows = mysqli_fetch_array($wallet_sql);
		        $wallet_balance = $wallet_rows['amount'] + $bet_amount;
		        $wallet_insert = mysqli_query($com,"update `wallet` set `amount`='$wallet_balance' where `userid`='$userid'");
	
		        $bet_updatess ="update `cricket_bet` set `result`='$value',`status`=1 where `userid`='$userid' and `over`='$over' and `ball`='$ball'";
		        $bet_success =  mysqli_query($com,$bet_updatess);
	
	            }else{
	   
                $bet_updateff ="update `cricket_bet` set `result`='$value',`status`=2 where `userid`='$userid' and `over`='$over' and `ball`='$ball'";
		        $bet_fail =  mysqli_query($com,$bet_updateff);
		
                  }
          }   
         
        $sql_insert =  mysqli_query($com, "insert into `cricket_result`(`result`,`over`,`ball`)values('$value','$over','$ball')");
       echo 'Published';
     }else{
         echo 'Already Result Published';
     }
     	 
 
?>

      
  