// Calculate days and price $pickup = new DateTime($pickup_date); $return = new DateTime($return_date); $interval = $pickup->diff($return); $total_days = $interval->days + 1; $total_price = $total_days * $car['price_per_day'];
$car = mysqli_fetch_assoc($result); $error = ''; $success = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') $username = mysqli_real_escape_string($conn, $_POST['username']); $password = $_POST['password'];
-- Cars table CREATE TABLE cars ( id INT PRIMARY KEY AUTO_INCREMENT, brand VARCHAR(50) NOT NULL, model VARCHAR(50) NOT NULL, year INT, license_plate VARCHAR(20) UNIQUE NOT NULL, color VARCHAR(30), seats INT DEFAULT 5, transmission ENUM('Manual', 'Automatic') DEFAULT 'Manual', fuel_type ENUM('Petrol', 'Diesel', 'Electric', 'Hybrid') DEFAULT 'Petrol', price_per_day DECIMAL(10,2) NOT NULL, image VARCHAR(255), status ENUM('available', 'rented', 'maintenance') DEFAULT 'available', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); car rental php script
<h2 class="mb-4">Available Cars</h2> <div class="row"> <?php if(mysqli_num_rows($result) > 0): ?> <?php while($car = mysqli_fetch_assoc($result)): ?> <div class="col-md-4 mb-4"> <div class="card h-100 shadow-sm"> <?php if($car['image']): ?> <img src="uploads/<?php echo $car['image']; ?>" class="card-img-top" alt="<?php echo $car['brand'] . ' ' . $car['model']; ?>" style="height: 200px; object-fit: cover;"> <?php else: ?> <div class="bg-secondary text-white text-center py-5"> <i class="fas fa-car fa-3x"></i> </div> <?php endif; ?> <div class="card-body"> <h5 class="card-title"><?php echo $car['brand'] . ' ' . $car['model'] . ' (' . $car['year'] . ')'; ?></h5> <p class="card-text"> <i class="fas fa-chair"></i> <?php echo $car['seats']; ?> Seats | <i class="fas fa-cog"></i> <?php echo $car['transmission']; ?> | <i class="fas fa-gas-pump"></i> <?php echo $car['fuel_type']; ?> </p> <p class="card-text"> <strong>License Plate:</strong> <?php echo $car['license_plate']; ?><br> <strong>Color:</strong> <?php echo $car['color']; ?> </p> <h4 class="text-primary">$<?php echo number_format($car['price_per_day'], 2); ?>/day</h4> </div> <div class="card-footer bg-transparent"> <?php if(isLoggedIn()): ?> <a href="book-car.php?id=<?php echo $car['id']; ?>" class="btn btn-primary w-100">Book Now</a> <?php else: ?> <a href="login.php" class="btn btn-primary w-100">Login to Book</a> <?php endif; ?> </div> </div> </div> <?php endwhile; ?> <?php else: ?> <div class="col-12"> <div class="alert alert-info">No cars available at the moment.</div> </div> <?php endif; ?> </div> </div>
// Function to check if user is logged in function isLoggedIn() return isset($_SESSION['user_id']);
-- Bookings table CREATE TABLE bookings ( id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, car_id INT, pickup_date DATE NOT NULL, return_date DATE NOT NULL, total_days INT, total_price DECIMAL(10,2), status ENUM('pending', 'confirmed', 'active', 'completed', 'cancelled') DEFAULT 'pending', payment_status ENUM('pending', 'paid', 'refunded') DEFAULT 'pending', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE ); // Calculate days and price $pickup = new
$check_result = mysqli_query($conn, $check_sql);
$car_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Register - <?php echo SITE_NAME; ?></title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body class="bg-light"> <div class="container mt-5"> <div class="row justify-content-center"> <div class="col-md-6"> <div class="card shadow"> <div class="card-header bg-primary text-white"> <h4 class="mb-0">Register</h4> </div> <div class="card-body"> <?php if($error) echo showMessage($error, 'danger'); ?> <?php if($success) echo showMessage($success, 'success'); ?> $car['year']
<div class="container mt-4"> <h2>My Bookings</h2> <?php if(mysqli_num_rows($result) > 0): ?> <div class="table-responsive"> <table class="table table-bordered table-hover"> <thead class="table-dark"> <tr> <th>Booking ID</th> <th>Car</th> <th>License Plate</th> <th>Pickup Date</th> <th>Return Date</th> <th>Total Days</th> <th>Total Price</th> <th>Status</th> <th>Payment Status</th> <th>Action</th> </tr> </thead> <tbody> <?php while($booking = mysqli_fetch_assoc($result)): ?> <tr> <td>#<?php echo $booking['id']; ?></td> <td><?php echo $booking['brand'] . ' ' . $booking['model']; ?></td> <td><?php echo $booking['license_plate']; ?></td> <td><?php echo date('d M Y', strtotime($booking['pickup_date'])); ?></td> <td><?php echo date('d M Y', strtotime($booking['return_date'])); ?></td> <td><?php echo $booking['total_days']; ?></td> <td>$<?php echo number_format($booking['total_price'], 2); ?></td> <td> <span class="badge bg-<?php echo $booking['status'] == 'confirmed' ? 'success' : ($booking['status'] == 'pending' ? 'warning' : ($booking['status'] == 'cancelled' ? 'danger' : 'info')); ?>"> <?php echo ucfirst($booking['status']); ?> </span> </td> <td> <span class="badge bg-<?php echo $booking['payment_status'] == 'paid' ? 'success' : 'warning'; ?>"> <?php echo ucfirst($booking['payment_status']); ?> </span> </td> <td> <?php if($booking['status'] == 'pending' && $booking['payment_status'] == 'pending'): ?> <a href="payment.php?booking_id=<?php echo $booking['id']; ?>" class="btn btn-sm btn-primary">Pay Now</a> <a href="cancel-booking.php?id=<?php echo $booking['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Cancel</a> <?php endif; ?> </td> </tr> <?php endwhile; ?> </tbody> </table> </div> <?php else: ?> <div class="alert alert-info">No bookings found.</div> <?php endif; ?> </div> </body> </html> <?php session_start(); session_destroy(); header("Location: index.php"); exit(); ?> This complete car rental system includes:
$error = ''; $success = '';