<?php include("../cn.php");

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");


$id = $_REQUEST["id"];

$dname = $_REQUEST["dname"];
$contact = $_REQUEST["contact"];
$dlnumber = $_REQUEST["dlNumber"];
$email = $_REQUEST["email"];
$password = $_REQUEST["Password"];
$address = $_REQUEST["address"];
// $gender = filter_input(INPUT_POST, 'gender', FILTER_SANITIZE_STRING);
$gender = $_REQUEST["gender"];
$Emailverified ="1";


$img_name = date("Ymdis");

$photo=($_FILES["photo"]["name"]!="")?"drivers/driver_".$img_name.".".pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION):"";
if($_FILES["photo"]["name"]!="")
{
    move_uploaded_file($_FILES["photo"]["tmp_name"],"../".$photo);
	$imgsql = "image='$photo',";

}

$query =  "UPDATE `driver` SET
$imgsql 
`driver_name`='$dname',
`dl_number`='$dlnumber',
`driver_phone`='$contact',
`driver_address`='$address',
`driver_gender`='$gender'
WHERE `driver_id`='$id'";

$result1 = mysqli_query($connect,$query);

$result2 = mysqli_query($connect,"SELECT * FROM app_users WHERE `driverid`='$id'");

// fwrite($myfile, $query);

// $connect->close();
// $connect=connect();

if(!mysqli_fetch_assoc($result2))
{

//     $query = "INSERT INTO `driver` (`driver_name`,`dl_number`,`driver_phone`, `Email`, 
// `driver_address`, `driver_gender`, `Image`, `driver_availability`) VALUES(
//  '$dname', $dlnumber, '$contact','$email','$address','$gender', '$photo', 'yes')";

$query2 = "INSERT INTO app_users (`name`,`password`,`phone`, `email`, `driverid`, `emailverified`) 
VALUES ( '$dname','$password','$contact','$email','$id','$Emailverified')";

fwrite($myfile, $query2);
// fwrite($myfile, $password);
// echo file_put_contents($myfile,$query);


$result2 = mysqli_query($connect, $query2);

if(!$result2)
    die( mysqli_error($connect));

}
else{
    $query = "UPDATE `app_users` SET 
    $imgsql
    `name` = '$dname',
    `password` = '$password',
    `phone` = '$contact',
    `email` = '$email',
    `emailverified` = '$Emailverified'
    WHERE
    `driverid` = '$id'";

    $result2 = mysqli_query($connect, $query);
}

fclose($myfile);
header("location:../drivers.php");

?>