<!–/ Form Search Star /–> <div class=“box-collapse”> <div class=“title-box-d”> <h3…
- User
<!–/ Form Search Star /–>
- Assistant
<!–/ Form Search Star /–>
To create a database and link it to your search form, you'll need to decide on the backend technology you'd like to use. For this example, I'll show you a simple way to achieve this using MySQL for the database and PHP for server-side scripting. I'll guide you through creating a MySQL database and table, inserting some sample data, and then querying that data based on your form input using PHP.
First, you'll need access to a MySQL database. Once you have that, create a new database and a table to hold your property data.
Use MySQL client or phpMyAdmin to create a new database:
CREATE DATABASE properties_db;
USE properties_db;
CREATE TABLE properties (
id INT AUTO_INCREMENT PRIMARY KEY,
keyword VARCHAR(255),
type VARCHAR(50),
city VARCHAR(50),
bedrooms INT,
garages INT,
bathrooms INT,
price DECIMAL
);
INSERT INTO properties (keyword, type, city, bedrooms, garages, bathrooms, price) VALUES
('Beautiful Villa', 'Per Month', 'Marrakech', 3, 2, 2, 2500),
('Cozy Apartment', 'Per Day', 'Casablanca', 2, 1, 1, 1200),
('Luxury House', 'Per Month', 'Fes', 4, 2, 3, 5000);
Assuming your form method is POST, you'll use PHP to retrieve form data and query the database to find matches. You’ll need to update the $host, $db, $user, and $pass variables with your own database details.
<?php
$host = "localhost"; // or your host
$db = "properties_db";
$user = "yourusername";
$pass = "yourpassword";
// Create connection
$conn = new mysqli($host, $user, $pass, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$type = $_POST['Type'] ?? '';
$city = $_POST['city'] ?? '';
$bedrooms = $_POST['bedrooms'] ?? '';
$garages = $_POST['garages'] ?? '';
$bathrooms = $_POST['bathrooms'] ?? '';
$price = $_POST['price'] ?? '';
// Build the SQL query
$sql = "SELECT * FROM properties WHERE
type LIKE '%$type%' AND
city LIKE '%$city%' AND
(bedrooms = '$bedrooms' OR '$bedrooms' = '') AND
(garages = '$garages' OR '$garages' = '') AND
(bathrooms = '$bathrooms' OR '$bathrooms' = '') AND
(price <= '$price' OR '$price' = '')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Property: " . $row["keyword"]. " " . $row["city"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Ensure your form's action attribute points to the location of this PHP script, or if the script is part of the same page, you can use action="".
This is a very basic example and doesn't include sanitization or prepared statements for the SQL queries, which you should implement to prevent SQL injection attacks. Also, you might need to adjust the code according to the exact configuration and requirements of your project.
Not yet included in a published release.