Viewing File: /home/quiczmwg/affordablerealtycorporation.com/admin/manage-rental-process.php
<?php
session_start();
include_once("../_db.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$bedrooms = $_POST['bedrooms'];
$location = $_POST['location'];
$baths = $_POST['baths'];
$heights = $_POST['heights'];
$description = $_POST['description'];
$price = $_POST['price'];
$nft_imgs = $_FILES['nft_img'];
// Validate each field individually
if (empty($bedrooms)) {
header('Location: manage-rental.php?status=error&message=Enter bedrooms');
exit();
}
if (empty($location)) {
header('Location: manage-rental.php?status=error&message=Enter location');
exit();
}
if (empty($baths)) {
header('Location: manage-rental.php?status=error&message=Enter baths');
exit();
}
if (empty($heights)) {
header('Location: manage-rental.php?status=error&message=Enter heights');
exit();
}
if (empty($description)) {
header('Location: manage-rental.php?status=error&message=Enter description');
exit();
}
if (empty($price)) {
header('Location: manage-rental.php?status=error&message=Enter price');
exit();
}
// Check if at least one image has been uploaded
if (empty(array_filter($nft_imgs['name']))) {
header('Location: manage-rental.php?status=error&message=Select at least one image');
exit();
}
// Generate unique rental ID
$rental_id = md5(uniqid());
// Process the file upload
$upload_directory = '../rentals/';
// Prepare the SQL statement
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$bedrooms = $_POST['bedrooms'];
$location = $_POST['location'];
$baths = $_POST['baths'];
$heights = $_POST['heights'];
$description = $_POST['description'];
$price = $_POST['price'];
$image = $_FILES['nft_img'];
// Check if all fields are filled
if (empty($bedrooms) || empty($location) || empty($image)) {
$status = 'error';
$message = 'Please fill all fields';
} else {
// File upload handling
$uploadDirectory = '../rental/';
$fileName = basename($image['name']);
$targetPath = $uploadDirectory . $fileName;
// Check if file already exists
if (file_exists($targetPath)) {
$status = 'error';
$message = 'File already exists';
} else {
// Upload file
if (!move_uploaded_file($image['tmp_name'], $targetPath)) {
$status = 'error';
$message = 'Failed to upload file';
} else {
// Insert into database
$insertQuery = "INSERT INTO rentals (rental_id, bedroom_category, location, baths, heights, description, price, nft_img) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$insertStmt = $conn->prepare($insertQuery);
// Check if prepare() was successful
if ($insertStmt) {
$insertStmt->bind_param("isssssds", $rental_id, $bedrooms, $location, $baths, $heights, $description, $price, $fileName);
// Assume rental_id is an auto-increment field, so no need to bind it or generate a value for it
// Execute Statement
if ($insertStmt->execute()) {
$status = 'success';
$message = 'Rental added successfully';
} else {
$status = 'error';
$message = 'Failed to execute insert query: ' . $insertStmt->error;
}
} else {
$status = 'error';
$message = 'Failed to prepare SQL statement: ' . $conn->error;
}
}
}
}
// Display status message
if ($status) {
echo '<div style="color: ' . ($status === 'success' ? 'green' : 'red') . ';">' . $message . '</div>';
}
}
// Loop through each image file
foreach ($nft_imgs['tmp_name'] as $key => $tmp_name) {
$nft_img_name = $nft_imgs['name'][$key];
// Move the uploaded file to the desired directory
if (move_uploaded_file($tmp_name, $upload_directory . $nft_img_name)) {
// Bind parameters and execute the statement
$insertStmt->bind_param("ssssssss", $rental_id, $bedrooms, $location, $baths, $heights, $description, $price, $nft_img_name);
$insertStmt->execute();
} else {
header('Location: manage-rental.php?status=error&message=Failed to move uploaded file');
exit();
}
}
// Close the prepared statement
$insertStmt->close();
// Redirect after successful insertion
header('Location: manage-rental.php?status=success&message=Rental added successfully');
exit();
} else {
header('Location: manage-rental.php?status=error&message=Invalid request');
exit();
}
?>
Back to Directory
File Manager
<