-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroker_post_listing.php
More file actions
111 lines (98 loc) · 3.45 KB
/
broker_post_listing.php
File metadata and controls
111 lines (98 loc) · 3.45 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'broker') {
header("Location: Practical_13_Login.html");
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "Heleninh@2022", "HAC_Services");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$title = $_POST['title'];
$description = $_POST['description'];
$location = $_POST['location'];
$price = $_POST['price'];
$broker_id = $_SESSION['user_id'];
// Inserting listing
$stmt = $conn->prepare("INSERT INTO listings (broker_id, title, description, location, price, status) VALUES (?, ?, ?, ?, ?, 'active')");
$stmt->bind_param("isssd", $broker_id, $title, $description, $location, $price);
$stmt->execute();
$listing_id = $stmt->insert_id;
$stmt->close();
// Uploading directory
$upload_dir = __DIR__ . "/Pictures";
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
foreach ($_FILES['images']['tmp_name'] as $index => $tmp_name) {
if ($_FILES['images']['error'][$index] === UPLOAD_ERR_OK) {
$original_name = basename($_FILES['images']['name'][$index]);
$unique_name = uniqid() . "_" . $original_name;
$target_path = $upload_dir . '/' . $unique_name;
if (move_uploaded_file($tmp_name, $target_path)) {
$stmt = $conn->prepare("INSERT INTO listing_images (listing_id, image_path) VALUES (?, ?)");
$stmt->bind_param("is", $listing_id, $unique_name);
$stmt->execute();
$stmt->close();
}
}
}
$conn->close();
header("Location: Practical_13_broker.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Post New Listing</title>
<style>
body { font-family: Arial; background: #f3f3f3; }
.form-container {
width: 600px;
margin: 50px auto;
background: white;
padding: 25px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
h2 { text-align: center; }
input, textarea {
width: 100%;
padding: 10px;
margin-top: 10px;
border-radius: 6px;
border: 1px solid #ccc;
}
button {
background: #4CAF50;
color: white;
padding: 10px 20px;
margin-top: 15px;
border: none;
border-radius: 5px;
cursor: pointer;
}
label { display: block; margin-top: 15px; font-weight: bold; }
</style>
</head>
<body>
<div class="form-container">
<h2>Post New Listing</h2>
<form method="post" action="broker_post_listing.php" enctype="multipart/form-data">
<label>Title:</label>
<input type="text" name="title" required>
<label>Description:</label>
<textarea name="description" required></textarea>
<label>Location:</label>
<input type="text" name="location" required>
<label>Price (Rs):</label>
<input type="number" name="price" step="0.01" required>
<label>Upload Images:</label>
<input type="file" name="images[]" multiple required>
<button type="submit">Post Listing</button>
</form>
</div>
</body>
</html>