<?php
if ($addflag != 1) {
    $compare1 = mysqli_query($conn, "SELECT MIN(`$keyfld`) AS mincode FROM `$maindb` WHERE `$keyfld` != '' $keycond LIMIT 1") or die(mysqli_error($conn));
    $fincode1 = mysqli_fetch_object($compare1);

    if ($fincode1 && $qvouch > $fincode1->mincode) {
        $chkqry    = mysqli_query($conn, "SELECT `$keyfld` FROM `$maindb` WHERE `$keyfld` < '$qvouch' $keycond ORDER BY `$keyfld` DESC LIMIT 1") or die(mysqli_error($conn));
        $chkcode   = mysqli_fetch_object($chkqry);
        $prevvouch = $chkcode ? $chkcode->$keyfld : $qvouch;
        $pdisabled = '';
    } else {
        $prevvouch  = $qvouch;
        $pdisabled1 = 'disabled';
    }

    $compare2 = mysqli_query($conn, "SELECT MAX(`$keyfld`) AS maxcode FROM `$maindb` WHERE `$keyfld` != '' $keycond LIMIT 1") or die(mysqli_error($conn));
    $fincode2 = mysqli_fetch_object($compare2);

    if ($fincode2 && $qvouch < $fincode2->maxcode) {
        $chkqryn   = mysqli_query($conn, "SELECT `$keyfld` FROM `$maindb` WHERE `$keyfld` > '$qvouch' $keycond ORDER BY `$keyfld` LIMIT 1") or die(mysqli_error($conn));
        $chkcoden  = mysqli_fetch_object($chkqryn);
        $nextvouch = $chkcoden ? $chkcoden->$keyfld : $qvouch;
        $ndisabled = '';
    } else {
        $nextvouch  = $qvouch;
        $ndisabled1 = 'disabled';
    }
}
