<?php 
include("config.php");
ob_start();
session_start();


$sql = mysqli_query($com,"select * from buy_3 where status='0'");

  while($row = mysqli_fetch_array($sql))
  {
     
      $userid = $row['userid'];
      $daily = $row['daily'];
      $day = $row['day'];
      $id = $row['id'];
      
      if($day == 45)
      {
          $sql_update = mysqli_query($com,"update buy_3 set status='1' where userid='$userid' and id='$id'");
      }else{
        $sql_update = mysqli_query($com,"update buy_3 set day = day + 1 where userid='$userid' and id='$id'");  
        $wallet = mysqli_query($com,"update withdraw_balance set amount = amount + $daily where userid='$userid'");
        $total_interest = mysqli_query($com,"update total_interest set amount = amount + $daily where userid='$userid'");
      date_default_timezone_set('Asia/Kolkata');
        $date = date('Y-m-d');
       $total_reword = mysqli_query($com,"insert into total_reword(userid,amount,date)values('$userid','$daily','$date')");
       
         $level1amount = $daily * 10/100;
       $level2amount = $daily * 5/100;
       $sqlus = mysqli_query($com,"select * from loginuser where id ='$userid'");
       $data = mysqli_fetch_assoc($sqlus);
       $code = $data['code'];
       if($code !='')
       {
         $sqluc = mysqli_query($com,"select * from loginuser where owncode ='$code'");
         $numc = mysqli_num_rows($sqluc);
         if($numc > 0)
         {
            $datac = mysqli_fetch_assoc($sqluc);
            $level1id = $datac['id'];
            $code1 = $datac['code'];
            
            $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$level1amount' where userid ='$level1id'"); 
            $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$level1amount','1','credit','bonus')");
            $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $level1amount,level1 = level1 + $level1amount where userid ='$level1id'");
            $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$level1amount')");
            if($code1 !='')   
            {
               $sqluc2 = mysqli_query($com,"select * from loginuser where owncode ='$code'");  
               $datac2 = mysqli_fetch_assoc($sqluc2);
               $level2id = $datac2['id'];
               $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$level2amount' where userid ='$level2id'"); 
               $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level2id','$level2amount','1','credit','bonus')");
               $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $level2amount,level2 = level2 + $level2amount where userid ='$level2id'");
               $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level2id,level2amount)values('$userid','$level2id','$level2amount')");
            
            }
         }
       }
     }
  }




?>