What are common issues when formulating SQL queries for a booking system in PHP?
Issue: One common issue when formulating SQL queries for a booking system in PHP is not properly handling user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements or parameterized queries to sanitize user input before including it in the SQL query.
// Example of using prepared statements to prevent SQL injection
// Assuming $conn is the database connection object
// User input
$user_id = $_POST['user_id'];
// Prepare the SQL statement
$stmt = $conn->prepare("SELECT * FROM bookings WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
// Execute the statement
$stmt->execute();
// Get the result
$result = $stmt->get_result();
// Process the result
while ($row = $result->fetch_assoc()) {
// Handle each row
}
// Close the statement and connection
$stmt->close();
$conn->close();
Related Questions
- How does PHP 5 handle Singleton attributes and why do they all have the same value in the provided code snippet?
- What best practices should be followed when modifying PHP code with conditional statements?
- What are the potential pitfalls of using the count() function within a while loop in PHP, especially when dealing with encrypted data?