forked from thejanRajapaksha/SW-Tools-and-Practices
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_listing.php
More file actions
86 lines (73 loc) · 2.86 KB
/
update_listing.php
File metadata and controls
86 lines (73 loc) · 2.86 KB
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
79
80
81
82
83
84
85
86
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve advertisement ID from the form
$advertisement_id = $_POST['advertisement_id'];
// Connect to the database
$servername = "localhost";
$username = "root";
$password = "";
$database = "accommodationfinder";
$connection = new mysqli($servername, $username, $password, $database);
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
// Prepare update query
$update_query = "UPDATE advertisements SET
title = ?,
rent = ?,
rooms = ?,
beds = ?,
longitude = ?,
latitude = ?";
// Check if a new image is uploaded
if ($_FILES['image']['error'] == UPLOAD_ERR_OK && $_FILES['image']['size'] > 0) {
// Specify the directory where you want to store the uploaded image
$uploadDirectory = "uploads/";
// Generate a unique filename to prevent overwriting existing files
$filename = uniqid() . "_" . basename($_FILES["image"]["name"]);
$targetFilePath = $uploadDirectory . $filename;
// Move the uploaded image to the target directory
if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFilePath)) {
// Image uploaded successfully
$image_path = $targetFilePath;
$update_query .= ", image_data = ?";
} else {
echo "Error moving uploaded file.";
exit; // Exit if there's an error moving the uploaded file
}
}
// Add the advertisement ID to the update query
$update_query .= " WHERE advertisement_id = ?";
// Prepare and bind parameters
$stmt = $connection->prepare($update_query);
if (!$stmt) {
die("Prepare failed: " . $connection->error);
}
// Bind parameters
if (isset($image_path)) {
$stmt->bind_param("sdiiddsi", $title, $rent, $rooms, $beds, $longitude, $latitude, $image_path, $advertisement_id);
} else {
$stmt->bind_param("sdiiddi", $title, $rent, $rooms, $beds, $longitude, $latitude, $advertisement_id);
}
// Set parameters
$title = $_POST['title'];
$rent = $_POST['rent'];
$rooms = $_POST['rooms'];
$beds = $_POST['beds'];
$longitude = $_POST['longitude']; // Assuming these are provided in the form
$latitude = $_POST['latitude']; // Assuming these are provided in the form
// Execute the update query
if ($stmt->execute()) {
echo "Advertisement updated successfully.";
header("Location: Show_listings.php");
exit();
} else {
echo "Error updating advertisement: " . $connection->error;
}
// Close the statement
$stmt->close();
// Close the database connection
$connection->close();
}
?>