151 lines
6.4 KiB
PHP
151 lines
6.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Ensure user is logged in and is a vendor
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'vendor') {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
$vendor_id = $_SESSION['user_id'];
|
|
$item_id = $_GET['id'] ?? null;
|
|
$item = null;
|
|
$errors = [];
|
|
$success_message = "";
|
|
|
|
$pdo = db();
|
|
|
|
// Fetch item details if ID is provided
|
|
if ($item_id) {
|
|
$stmt = $pdo->prepare("SELECT * FROM items WHERE id = ? AND owner_id = ?");
|
|
$stmt->execute([$item_id, $vendor_id]);
|
|
$item = $stmt->fetch();
|
|
|
|
if (!$item) {
|
|
$errors[] = "Item not found or you don't have permission to edit it.";
|
|
$item_id = null; // Invalidate item_id if not found or unauthorized
|
|
}
|
|
} else {
|
|
$errors[] = "No item ID provided.";
|
|
}
|
|
|
|
// Handle form submission for updating the item
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && $item_id) {
|
|
$name = trim($_POST['name']);
|
|
$description = trim($_POST['description']);
|
|
$price_per_day = trim($_POST['price_per_day']);
|
|
$location = trim($_POST['location']);
|
|
$image_url = trim($_POST['image_url']);
|
|
|
|
if (empty($name)) {
|
|
$errors[] = "Item name is required.";
|
|
}
|
|
if (empty($price_per_day) || !is_numeric($price_per_day)) {
|
|
$errors[] = "A valid price is required.";
|
|
}
|
|
if (empty($location)) {
|
|
$errors[] = "Location is required.";
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$sql = "UPDATE items SET name = :name, description = :description, price_per_day = :price_per_day, location = :location, image_url = :image_url WHERE id = :id AND owner_id = :owner_id";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':name' => $name,
|
|
':description' => $description,
|
|
':price_per_day' => $price_per_day,
|
|
':location' => $location,
|
|
':image_url' => $image_url,
|
|
':id' => $item_id,
|
|
':owner_id' => $vendor_id
|
|
]);
|
|
$success_message = "Item updated successfully!";
|
|
// Refresh item data after update
|
|
$stmt = $pdo->prepare("SELECT * FROM items WHERE id = ? AND owner_id = ?");
|
|
$stmt->execute([$item_id, $vendor_id]);
|
|
$item = $stmt->fetch();
|
|
|
|
} catch (PDOException $e) {
|
|
$errors[] = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Edit Item - RentEase</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
|
|
<?php include 'header.php'; ?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Edit Rental Item</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($success_message)): ?>
|
|
<div class="alert alert-success">
|
|
<?php echo htmlspecialchars($success_message); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($item): ?>
|
|
<form action="edit_item.php?id=<?php echo htmlspecialchars($item['id']); ?>" method="post">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Item Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($item['name']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"><?php echo htmlspecialchars($item['description']); ?></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="price_per_day" class="form-label">Price per Day ($)</label>
|
|
<input type="number" class="form-control" id="price_per_day" name="price_per_day" step="0.01" value="<?php echo htmlspecialchars($item['price_per_day']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="location" class="form-label">Location (e.g., City, State)</label>
|
|
<input type="text" class="form-control" id="location" name="location" value="<?php echo htmlspecialchars($item['location']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="image_url" class="form-label">Image URL</label>
|
|
<input type="text" class="form-control" id="image_url" name="image_url" value="<?php echo htmlspecialchars($item['image_url'] ?? ''); ?>">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Update Item</button>
|
|
<a href="my_listings.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
<?php else: ?>
|
|
<p>Unable to load item for editing.</p>
|
|
<a href="my_listings.php" class="btn btn-primary">Back to My Listings</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'footer.php'; ?>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|