What are the potential pitfalls of using query parameters to control the number of entries displayed per page in PHP?

One potential pitfall of using query parameters to control the number of entries displayed per page in PHP is the lack of input validation, which can lead to security vulnerabilities such as SQL injection attacks. To mitigate this risk, it is important to sanitize and validate the input before using it in a query. This can be done by using prepared statements or parameterized queries to prevent malicious input from affecting the database.

// Sanitize and validate the input for the number of entries per page
$entries_per_page = isset($_GET['entries_per_page']) ? intval($_GET['entries_per_page']) : 10;
if($entries_per_page < 1 || $entries_per_page > 100) {
    $entries_per_page = 10; // Set a default value if input is invalid
}

// Use prepared statements to safely query the database
$stmt = $pdo->prepare("SELECT * FROM your_table LIMIT :limit");
$stmt->bindParam(':limit', $entries_per_page, PDO::PARAM_INT);
$stmt->execute();

// Fetch and display the results
while($row = $stmt->fetch()) {
    // Display the data
}