prepare($sql); $stmt->bindParam(':name', $name, PDO::PARAM_STR); $stmt->bindParam(':address', $address, PDO::PARAM_STR); $stmt->bindParam(':rent_amount', $rent_amount, PDO::PARAM_STR); $stmt->execute(); $property_id = $db->lastInsertId(); // Get the ID of the new property // Handle file upload if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) { $target_dir = "uploads/"; if (!is_dir($target_dir)) { mkdir($target_dir, 0755, true); } $file_name = uniqid() . '-' . basename($_FILES["file"]["name"]); $target_file = $target_dir . $file_name; if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { // Insert file info into database $file_sql = "INSERT INTO files (file_name, file_path, property_id) VALUES (:file_name, :file_path, :property_id)"; $file_stmt = $db->prepare($file_sql); $file_stmt->bindParam(':file_name', $file_name, PDO::PARAM_STR); $file_stmt->bindParam(':file_path', $target_file, PDO::PARAM_STR); $file_stmt->bindParam(':property_id', $property_id, PDO::PARAM_INT); $file_stmt->execute(); } else { $errors[] = "Sorry, there was an error uploading your file."; // Note: The property has been added, but the file upload failed. // You might want to handle this case, e.g., by showing a specific error message. } } header('Location: properties.php?message=Property added successfully.'); exit; } catch (Exception $e) { $errors[] = "DB ERROR: " . $e->getMessage(); } } } include 'templates/header.php'; ?>

Add New Property

Cancel