<?php

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

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

$maindb = 'latefees';
$accdb = 'actran';
$myyear = $_SESSION[$myyear];

$source = 'SR';
$purcode = '9999915';

$result = mysqli_query($conn, "SELECT * FROM `bookform` order by vouchno asc") or die(mysqli_error($conn));
$zroot = $_SESSION["zdir"];

//print table($result);
//die();
$getnum = mysqli_num_rows($result);
//browse($getnum);
$mvouch = 0;
while ($get = mysqli_fetch_array($result)) {

    $fname = $get['fname'];
    $fmobile = $get['mobile'];

    $chkadd = mysqli_query($conn, "SELECT accode FROM $zroot.address WHERE name = '$fname' and mobile = '$fmobile'") or die(mysqli_error($conn));
    $mnoad = mysqli_num_rows($chkadd);
    if ($mnoad <= 0) {

        $gena = mysqli_query($conn, "SELECT MAX(accode) AS maxcode FROM  $zroot.address where accode != '' LIMIT 0,1 ") or die(mysqli_error($conn));
        if (mysqli_num_rows($gena) > 0) {
            $vvoucha = mysqli_fetch_assoc($gena);
            $qaccode = $vvoucha["maxcode"] + 1;
        } else {
            $qaccode = '51';
        }

        $add1 = mysqli_real_escape_string($conn, $get['add1']);
        $add2 = mysqli_real_escape_string($conn, $get['add2']);
        $add3 = mysqli_real_escape_string($conn, $get['add3']);

        mysqli_query($conn, "INSERT INTO $zroot.address(`yearno`, `tour`, `regnno`, `grpcode`, `accode`, `name`, `nameprin`, 
                                                            `init`, `add1`, `add2`, `add3`, `city`, `pin`,
                                                            `tel`, `mobile`, `email`,`state`) 
                                                    VALUES ('2023', '$get[tourno]', '$get[regnno]', '010', '$qaccode', '$get[fname]', '$get[fname]', 
                                                            'SDR', '$add1', '$add2', '$add3', '$get[city]', '$get[pin]',
                                                            '$get[tel]', '$get[mobile]', '$get[email]', '$get[state]')") or die(mysqli_error($conn));
    }


    $sname = $get['sname'];
    $smobile = $get['mobile2'];

    if ($sname != "") {
        $chkadd2 = mysqli_query($conn, "SELECT accode FROM $zroot.address WHERE name = '$sname' and mobile = '$smobile'") or die(mysqli_error($conn));
        $mnoad2 = mysqli_num_rows($chkadd2);
        if ($mnoad2 <= 0) {

            $gena2 = mysqli_query($conn, "SELECT MAX(accode) AS maxcode FROM  $zroot.address where accode != '' LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gena2) > 0) {
                $vvoucha2 = mysqli_fetch_assoc($gena2);
                $qaccode2 = $vvoucha2["maxcode"] + 1;
            } else {
                $qaccode2 = '51';
            }

            $sadd1 = mysqli_real_escape_string($conn, $get['sadd1']);
            $sadd2 = mysqli_real_escape_string($conn, $get['sadd2']);
            $sadd3 = mysqli_real_escape_string($conn, $get['sadd3']);
            mysqli_query($conn, "INSERT INTO $zroot.address(`yearno`, `tour`, `regnno`, `grpcode`, `accode`, `name`, `nameprin`, 
                                                            `init`, `add1`, `add2`, `add3`, `city`, `pin`,
                                                            `tel`, `mobile`, `email`,`state`) 
                                                    VALUES ('2023', '$get[tourno]', '$get[regnno]', '010', '$qaccode2', '$get[sname]', '$get[sname]', 
                                                            'SDR', '$sadd1', '$sadd2', '$sadd3', '$get[city2]', '$get[pin2]',
                                                            '$get[tel2]', '$get[mobile2]', '$get[email2]', '$get[state2]')") or die(mysqli_error($conn));
        }
    }
}
browse("success");
?>