Вызывается ваш ввод изображения в комнате img_url2
, с которым вы никогда не взаимодействуете в коде.
Для лучшей практики переместите основную часть вашего PHP-кода в функцию загрузки файлов, затем вызовите его один раз с img_url
помощью ввода $ _FILE и один раз img_url2
.
Пример:
<?php
error_reporting(E_ALL);
ini_set("display_errors", true);
ini_set("display_startup_errors", true);
function uploadFile($file, $path)
{
$img_url_name = "";
if(is_array($file) && isset($file['name']) && isset($file['tmp_name']) && isset($file['size']))
{
$tmpFilePath = isset($file['tmp_name'][0]) ? $file['tmp_name'][0] : "";
$name = isset($file['name'][0]) ? $file['name'][0] : "";
list($txt, $ext) = explode(".", $name);
$file = time().substr(str_replace(" ", "_", $txt), 0);
$filename = $file.".".$ext;
$filepath_upload = $path.$filename;
$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
return "<div class='alert alert-success'>Sorry, ".$name." Image files Extension .".$ext." are not Uploaded & Other files Uploaded Successfully <br/>.</div>" ;
}
else if(move_uploaded_file($tmpFilePath, $filepath_upload))
{
$img_url_name .= $filename."|";
}
$filepath = rtrim($img_url_name, '|');
if(!empty($filepath))
{
if(!isset($con))
{
global $con;
}
$query = mysqli_query($con, "INSERT INTO `hotel`(`img_url`, `room_url`) VALUES ('".$filename."','".$filepath."')");
if($query)
{
return "<div class='alert alert-success'>Hotel Record Added Successfully</div>";
}
}
}
return "<div class='alert alert-success'>Unable to Add Please Try Again !! </div>";
}
if(isset($_POST['submit']))
{
if(isset($_FILES['img_url']['name']))
{
echo uploadFile($_FILES['img_url'], "../images/hotel_images/");
}
if(isset($_FILES['img_url2']['name']))
{
echo uploadFile($_FILES['img_url2'], "../images/room_img/");
}
}
?>
<form action="" method="POST" enctype="multipart/form-data" id="add-hotel-form">
<div class="form-group col-md-12">
<label for="hotel">Upload Hotel Images</label>
<input type="file" class="form-control-file" id="upload_hotel_img1" name="img_url" multiple>
</div>
<div class="form-group col-md-12">
<label for="room">Upload Room Images</label>
<input type="file" class="form-control-file" id="upload_room_img1" name="img_url2" multiple>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>