-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit_item.php
More file actions
122 lines (106 loc) · 3.33 KB
/
submit_item.php
File metadata and controls
122 lines (106 loc) · 3.33 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
112
113
114
115
116
117
118
119
120
121
122
<?php
session_start();
if (empty($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$imagePath = '';
$user_id = $_SESSION['user_id'];
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/';
$filename = uniqid() . '_' . basename($_FILES['image']['name']);
$targetPath = $uploadDir . $filename;
if (move_uploaded_file($_FILES['image']['tmp_name'], $targetPath)) {
$imagePath = $targetPath;
}
}
try {
$stmt = $pdo->prepare("INSERT INTO items (title, description, price, image_url, user_id) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$title, $description, $price, $imagePath, $user_id]);
header("Location: index.php");
exit;
} catch (PDOException $e) {
die("Error inserting item: " . $e->getMessage());
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Post New Item</title>
<link rel="stylesheet" href="style.css">
<style>
.form-group {
margin-bottom: 20px;
}
label {
font-weight: bold;
display: block;
margin-bottom: 6px;
color: #444;
}
#filename {
margin-left: 10px;
color: #555;
font-size: 14px;
}
.custom-upload-btn {
background-color: #ffe5f2;
color: #cc0077;
border: 1.5px solid #ff80bf;
padding: 6px 14px;
border-radius: 24px;
font-size: 13px;
font-weight: bold;
cursor: pointer;
transition: all 0.2s ease-in-out;
display: inline-block;
}
.custom-upload-btn:hover {
background-color: #ffd6eb;
border-color: #ff66aa;
color: #b3005f;
}
</style>
</head>
<body>
<div class="container">
<h1>Post New Item</h1>
<form method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Title:</label>
<input type="text" name="title" required>
</div>
<div class="form-group">
<label>Description:</label>
<textarea name="description" rows="5" required></textarea>
</div>
<div class="form-group">
<label>Price:</label>
<input type="number" step="0.01" name="price" required>
</div>
<div class="form-group">
<label>Upload picture:</label>
<label for="image" class="custom-upload-btn">Choose File</label>
<span id="filename">No file selected</span>
<input type="file" name="image" id="image" accept="image/*" style="display: none;">
</div>
<button type="submit">Submit</button>
</form>
<p><a href="index.php">← Back to Marketplace</a></p>
</div>
<script>
const input = document.getElementById('image');
const filenameDisplay = document.getElementById('filename');
input.addEventListener('change', function () {
filenameDisplay.textContent = input.files.length > 0 ? input.files[0].name : 'No file selected';
});
</script>
</body>
</html>