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

$carname = $_REQUEST["title"];
$model = $_REQUEST["model"];
$brandname = $_REQUEST["brand"];
$plate = $_REQUEST["plate"];
$dailyrate = floatval($_REQUEST["dailyrate"]);
$ratepermin = floatval($_REQUEST["ratepermin"]);
$availability = "yes";

$query = "SELECT * FROM `cars`WHERE `car_nameplate`= '$plate'";

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

if(mysqli_fetch_assoc($result)){
        http_response_code(200);
        echo ('Car with this Name Plate/Number Plate Already exists');
        exit;
	 }
	 
$img_name = date("Ymdis");
$photo=($_FILES["photo"]["name"]!="")?"cars/car_".$img_name.".".pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION):"";
if($_FILES["photo"]["name"]!="")
{
    move_uploaded_file($_FILES["photo"]["tmp_name"],"../".$photo);

}	 
	 
/*$img_name = date("Ymdis");
$photo=($_FILES["photo"]["name"]!="")?"assets/img/cars/cars_".$img_name.".".pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION):"";

if($_FILES["photo"]["name"]!="")
{
    move_uploaded_file($_FILES["photo"]["tmp_name"],"/booking/assets/img/cars/".$photo);
}*/

$query = "INSERT INTO `cars`(`car_name`,`model`,`car_nameplate`,`ac_price_per_day`,`ac_price`,`car_img`,`brand`,`car_availability`)
VALUES('$carname','$model','$plate','$dailyrate','$ratepermin', '$photo','$brandname','$availability')";

$myfile = fopen("testfile.txt", "w");
fwrite($myfile, $query);
fclose($myfile);

mysqli_query($connect, $query);

header("location:../cars.php");

?>