Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
-- Create bookings table CREATE TABLE IF NOT EXISTS bookings ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, phone VARCHAR(20) NOT NULL, date DATE NOT NULL, time TIME NOT NULL, guests INT NOT NULL, notes TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php // Function to sanitize input with null checks function sanitize($data) { if ($data === null) { return ''; } $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } // Create table if not exists try { $sql = "CREATE TABLE IF NOT EXISTS bookings ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, phone VARCHAR(20) NOT NULL, date DATE NOT NULL, time TIME NOT NULL, guests INT NOT NULL, notes TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; $pdo->exec($sql); } catch (PDOException $e) { echo "Table creation error: " . $e->getMessage(); } // Process form submission $message = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get form data with null checks $name = isset($_POST["name"]) ? sanitize($_POST["name"]) : ''; $email = isset($_POST["email"]) ? sanitize($_POST["email"]) : ''; $phone = isset($_POST["phone"]) ? sanitize($_POST["phone"]) : ''; $date = isset($_POST["date"]) ? sanitize($_POST["date"]) : ''; $time = isset($_POST["time"]) ? sanitize($_POST["time"]) : ''; $guests = isset($_POST["guests"]) ? sanitize($_POST["guests"]) : ''; $notes = isset($_POST["notes"]) ? sanitize($_POST["notes"]) : ''; // Insert booking into database try { $sql = "INSERT INTO bookings (name, email, phone, date, time, guests, notes, created_at) VALUES (:name, :email, :phone, :date, :time, :guests, :notes, NOW())"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':name' => $name, ':email' => $email, ':phone' => $phone, ':date' => $date, ':time' => $time, ':guests' => $guests, ':notes' => $notes ]); $message = "Booking successfully created!"; } catch (PDOException $e) { $message = "Error: " . $e->getMessage(); } } // Get all bookings $bookings = []; try { $stmt = $pdo->query("SELECT * FROM bookings ORDER BY date, time"); $bookings = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $message = "Error retrieving bookings: " . $e->getMessage(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Restaurant Booking System</title> <style> body { font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px; background-color: #f4f4f4; } .container { max-width: 1200px; margin: 0 auto; background: white; padding: 20px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } h1, h2 { color: #333; } .form-group { margin-bottom: 15px; } label { display: block; margin-bottom: 5px; font-weight: bold; } input, select, textarea { width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px; } button { background: #4CAF50; color: white; border: none; padding: 10px 15px; border-radius: 4px; cursor: pointer; } button:hover { background: #45a049; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } table, th, td { border: 1px solid #ddd; } th, td { padding: 12px; text-align: left; } th { background-color: #f2f2f2; } .message { padding: 10px; margin-bottom: 20px; background-color: #dff0d8; border: 1px solid #d6e9c6; color: #3c763d; border-radius: 4px; } </style> </head> <body> <div class="container"> <h1>Restaurant Table Booking System</h1> <?php if (!empty($message)): ?> <div class="message"><?php echo $message; ?></div> <?php endif; ?> <div class="booking-form"> <h2>Make a Reservation</h2> <form method="POST"> <div class="form-group"> <label for="name">Name</label> <input type="text" id="name" name="name" required> </div> <div class="form-group"> <label for="email">Email</label> <input type="email" id="email" name="email" required> </div> <div class="form-group"> <label for="phone">Phone</label> <input type="tel" id="phone" name="phone" required> </div> <div class="form-group"> <label for="date">Date</label> <input type="date" id="date" name="date" required> </div> <div class="form-group"> <label for="time">Time</label> <input type="time" id="time" name="time" required> </div> <div class="form-group"> <label for="guests">Number of Guests</label> <select id="guests" name="guests" required> <?php for ($i = 1; $i <= 10; $i++): ?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option> <?php endfor; ?> </select> </div> <div class="form-group"> <label for="notes">Special Requests</label> <textarea id="notes" name="notes" rows="3"></textarea> </div> <button type="submit">Book Table</button> </form> </div> <div class="bookings-list"> <h2>Current Bookings</h2> <?php if (!empty($bookings)): ?> <table> <thead> <tr> <th>Name</th> <th>Date</th> <th>Time</th> <th>Guests</th> <th>Contact</th> <th>Notes</th> </tr> </thead> <tbody> <?php foreach ($bookings as $booking): ?> <tr> <td><?php echo htmlspecialchars($booking['name']); ?></td> <td><?php echo htmlspecialchars($booking['date']); ?></td> <td><?php echo htmlspecialchars($booking['time']); ?></td> <td><?php echo htmlspecialchars($booking['guests']); ?></td> <td> <?php echo htmlspecialchars($booking['email']); ?><br> <?php echo htmlspecialchars($booking['phone']); ?> </td> <td><?php echo htmlspecialchars($booking['notes']); ?></td> </tr> <?php endforeach; ?> </tbody> </table> <?php else: ?> <p>No bookings found.</p> <?php endif; ?> </div> </div> </body> </html>
Copy Clear