<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);


if (isset($_REQUEST['code'])) {

    $name = mysqli_real_escape_string($conn, $_REQUEST['name']);
    $code = mysqli_real_escape_string($conn, $_REQUEST['code']);


        $result = mysqli_query($conn, "SELECT * FROM `visatype` WHERE `name`= '$name'") or die(mysqli_error($conn));
        $getnum = mysqli_num_rows($result);

        if ($getnum > 0) {
            echo "Already Exist.";
        } else {
            $gen1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `visatype` LIMIT 0,1 ") or die(mysqli_error($conn));

            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }

            mysqli_query($conn, "INSERT INTO `visatype`(`code`, `name`)
                                        VALUES($qvouch, '$name')") or die(mysqli_error($conn));
        }

        Echo "<option value='$name'>$name</option>";
        $gdata = mysqli_query($conn, "SELECT name FROM `visatype` order by name")or die(mysqli_error($conn));
        while ($data = mysqli_fetch_array($gdata)) {
            Echo "<option value='$data[name]'>$data[name]</option>";
        }
    
}
?>
