How can the issue of multiple table rows being created for the same customer reservation be addressed in PHP?
Issue: The problem of multiple table rows being created for the same customer reservation can be addressed by checking if a reservation already exists for the customer before inserting a new row into the database. This can be achieved by querying the database for existing reservations based on the customer's unique identifier (e.g., email or customer ID) and only inserting a new row if no existing reservation is found. PHP Code Snippet:
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check if a reservation already exists for the customer
$customer_email = "customer@example.com";
$sql = "SELECT * FROM reservations WHERE customer_email = '$customer_email'";
$result = $conn->query($sql);
if ($result->num_rows == 0) {
// Insert a new reservation for the customer
$reservation_date = "2022-01-01";
$reservation_time = "12:00 PM";
$insert_sql = "INSERT INTO reservations (customer_email, reservation_date, reservation_time) VALUES ('$customer_email', '$reservation_date', '$reservation_time')";
if ($conn->query($insert_sql) === TRUE) {
echo "New reservation created successfully";
} else {
echo "Error: " . $insert_sql . "<br>" . $conn->error;
}
} else {
echo "A reservation already exists for this customer";
}
// Close the database connection
$conn->close();