<?php
ob_start();
session_start();

include("config.php");

$code = $_POST['code'];

$sql = mysqli_query($com,"select * from obtain where code='$code'");
$num = mysqli_num_rows($sql);
if($num !=0)
{
    $data = mysqli_fetch_assoc($sql);
    $noofuser = $data['noofuser'];
    $count = $data['count'];
    $amount = $data['amount'];
    if($noofuser != $count)
    {
        $update = mysqli_query($com,"update obtain set count=count + 1 where code='$code'");
        $userid =  $_SESSION['frontuserid'];
        $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount= amount + $amount where userid ='$userid'"); 
        $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','$amount','1','credit','bonus')");
        $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + $amount where userid ='$userid'"); 
                
    }else{
     echo "<script>
    alert('Code Expired');
    window.location.href='obtain_bonus.php';
          </script>";   
    }
    
}else{
    echo "<script>
    alert('Wrong Code');
    window.location.href='obtain_bonus.php';
          </script>";
          
    }
?>