-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_listing.php
78 lines (67 loc) · 2.8 KB
/
add_listing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
session_start();
// Check if user is logged in
if (!isset($_SESSION["landlord_id"])) {
header("Location: login.html");
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_listing'])) {
// Validate inputs
$title = $_POST["title"];
$rent = $_POST["rent"];
$rooms = $_POST["rooms"];
$beds = $_POST["beds"];
$longitude = $_POST["longitude"];
$latitude = $_POST["latitude"];
if (empty($title) || empty($rent) || empty($rooms) || empty($beds) || empty($longitude) || empty($latitude)) {
echo "<script>alert('All fields are required.');</script>";
} else {
// Check if image file is uploaded
if (isset($_FILES['image']) && !empty($_FILES['image']['tmp_name'])) {
// Define folder where images will be stored
$uploadDirectory = "uploads/";
// Generate a unique filename to prevent overwriting existing files
$filename = uniqid() . "_" . basename($_FILES["image"]["name"]);
$targetFilePath = $uploadDirectory . $filename;
// Check if the file has been uploaded successfully
if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFilePath)) {
// Image uploaded successfully, store the path in the database
$image_data = $targetFilePath;
} else {
echo "<script>alert('Error uploading image.');</script>";
exit();
}
} else {
echo "<script>alert('Please select an image.');</script>";
exit();
}
// Connect to the database (Replace with your database details)
$servername = "localhost";
$db_username = "root";
$db_password = "";
$dbname = "accommodationfinder";
$conn = new mysqli($servername, $db_username, $db_password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and execute SQL query to insert listing into database
$sql = "INSERT INTO advertisements (title, rent, rooms, beds, longitude, latitude, image_data, landlord_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Error preparing statement: " . $conn->error);
}
// Bind parameters
$stmt->bind_param("sdiiddsi", $title, $rent, $rooms, $beds, $longitude, $latitude, $image_data, $_SESSION["landlord_id"]);
// Execute the statement
if ($stmt->execute()) {
echo "<script>alert('Listing added successfully!');</script>";
header("Location: landlord_prop.html");
exit();
} else {
echo "Error executing statement: " . $stmt->error;
}
$stmt->close();
$conn->close();
}
}
?>