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

$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);

mysqli_select_db($conn,$zdb);
$success    = "";
$snapVouch  = $_GET["snapVouch"];
$snapgive   = $_GET["snapgive"];
$snapLast   = $_GET["snapLast"];
$snapPass   = $_GET["snapPass"];
$snapType   = $_GET["snapType"];
$mySnap     = $_GET["mySnap"];
$path       = "passImage/";
if($snapPass != "") {

    $fileName = "V" . $snapVouch . "_" . $snapPass . "_" . $snapType . ".jpg";
    $img_file = $path . ($fileName);

    if (move_uploaded_file($_FILES['webcam']['tmp_name'], $img_file)) {

        echo "$fileName upload is complete";

        ###########################SAVE PART################################################   
        $imgp = mysqli_query($conn,"SELECT * FROM `passimage`")or die(mysqli_error($conn));
        $gimg = mysqli_fetch_array($imgp);

        mysqli_query($conn,"DELETE FROM `passimage` WHERE vouchno='$snapVouch' AND `type`='$snapType' AND `passno`='$snapPass'") or die(mysqli_error($conn));

        if ($gimg[vouchno] == $snapVouch && $gimg[type] == $snapType && $gimg[passno] == $snapPass) {
            mysqli_query($conn,"UPDATE `passimage` SET `vouchno`='$snapVouch',`type`='$snapType',`passno`='$snapPass',`paxLast`='$snapLast',`paxGive`='$snapGive', `imgpath`='$img_file' WHERE `chalno`='$chalno' AND `series`='$series'") or die(mysqli_error($conn));
        }

        mysqli_query($conn,"INSERT INTO `passimage`(`vouchno`,`type`,`passno`,`givename`,`lastname`,`imgpath`)
                            VALUES('$snapVouch','$snapType','$snapPass','$snapGive','$snapLast','$img_file')") or die(mysqli_error($conn));
    } else {
        echo "move_uploaded_file function failed";
    }
}
?>