<?php
include ('database.php');
session_start();



if(isset($_POST['submit']))
{
   $vcode = mysqli_real_escape_string($connection, $_POST['vcode']);
   $verify_query = "SELECT vcode, verified, id FROM users WHERE vcode='$vcode' LIMIT 1";
   $verify_query_run = mysqli_query($connection, $verify_query);

   if(mysqli_num_rows($verify_query_run) > 0)
   {
      $row = mysqli_fetch_array($verify_query_run);
      if($row['verified'] == 0)
      {
          $id= $row['id'];
          $update_query = "UPDATE users SET verified= '1' WHERE id='$id' LIMIT 1";
          $update_query_run = mysqli_query($connection, $update_query);

          if($update_query_run)
          {
              $_SESSION['id'] = $id;
              header("location: ../enter-code.php");
              exit(0);
          }
          else
          {
            $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>Account not verified</h5>";
            header("location: ../../verify.php");
              exit(0);  
          }
      }
      else
      {
        $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>Email already verified. please login</h5>";
        header("location: ../../verify.php");
              exit(0);   
      }
   }
   else
   {
    $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>This token does not exists</h5>";
    header("location: ../../verify.php");
    exit(0);
   }
}
else
{
    $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>Account Not Verified</h5>";
    header("location: ../../verify.php");
}



?>