34968-vm/profile.php
Flatlogic Bot ef71b241ae V8
2025-10-15 02:30:11 +00:00

88 lines
3.4 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
include 'header.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['user_id'];
$db = db();
// Handle profile update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$address = trim($_POST['address']);
if (empty($name) || empty($email)) {
$error = "Name and email are required.";
} else {
$p_update = $db->prepare("UPDATE users SET name = ?, email = ?, address = ? WHERE id = ?");
$p_update->execute([$name, $email, $address, $user_id]);
$success = "Profile updated successfully!";
}
}
// Fetch user data
$p_user = $db->prepare("SELECT * FROM users WHERE id = ?");
$p_user->execute([$user_id]);
$user = $p_user->fetch();
// Fetch user's orders
$p_orders = $db->prepare("SELECT o.*, r.name as restaurant_name FROM orders o JOIN restaurants r ON o.restaurant_id = r.id WHERE o.user_id = ? ORDER BY o.created_at DESC");
$p_orders->execute([$user_id]);
$orders = $p_orders->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="container mt-5">
<h2>My Profile</h2>
<hr>
<?php if (isset($success)): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST" action="profile.php">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($user['name']); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<textarea class="form-control" id="address" name="address" rows="3"><?php echo htmlspecialchars($user['address']); ?></textarea>
</div>
<button type="submit" name="update_profile" class="btn btn-primary">Update Profile</button>
</form>
<h2 class="mt-5">My Orders</h2>
<hr>
<?php if (count($orders) > 0): ?>
<div class="list-group">
<?php foreach ($orders as $order): ?>
<a href="order_details.php?id=<?php echo $order['id']; ?>" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">Order #<?php echo $order['id']; ?> - <?php echo htmlspecialchars($order['restaurant_name']); ?></h5>
<small><?php echo date("F j, Y, g:i a", strtotime($order['created_at'])); ?></small>
</div>
<p class="mb-1">Total: $<?php echo number_format($order['total_price'], 2); ?></p>
<small>Status: <?php echo htmlspecialchars($order['status']); ?></small>
</a>
<?php endforeach; ?>
</div>
<?php else: ?>
<p>You have no past orders.</p>
<?php endif; ?>
</div>
<?php include 'footer.php'; ?>