36874-vm/add_item.php
Flatlogic Bot 1d182fef38 1.1
2025-12-11 19:19:52 +00:00

110 lines
4.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'];
$name = $description = $price_per_day = $location = "";
$errors = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST['name']);
$description = trim($_POST['description']);
$price_per_day = trim($_POST['price_per_day']);
$location = trim($_POST['location']);
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 {
$pdo = db();
$sql = "INSERT INTO items (vendor_id, name, description, price_per_day, location) VALUES (:vendor_id, :name, :description, :price_per_day, :location)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':vendor_id' => $vendor_id,
':name' => $name,
':description' => $description,
':price_per_day' => $price_per_day,
':location' => $location
]);
header("Location: dashboard.php?item_added=true");
exit();
} 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>Add New 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>Add a New 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 $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="add_item.php" 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($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($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($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($location); ?>" required>
</div>
<button type="submit" class="btn btn-primary">Add Item</button>
</form>
</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>