How can PHP be used to dynamically generate links for database queries based on user input?

When a user inputs search criteria on a website, PHP can be used to dynamically generate SQL queries to retrieve data from a database based on that input. This involves taking the user input, constructing a SQL query string with placeholders for the input values, binding the input values to the query, executing the query, and displaying the results.

<?php
// Assuming user input is stored in variables $searchTerm1 and $searchTerm2
$searchTerm1 = $_POST['searchTerm1'];
$searchTerm2 = $_POST['searchTerm2'];

// Connect to database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare SQL query with placeholders
$sql = "SELECT * FROM mytable WHERE column1 = :searchTerm1 AND column2 = :searchTerm2";
$stmt = $pdo->prepare($sql);

// Bind input values to the query
$stmt->bindParam(':searchTerm1', $searchTerm1);
$stmt->bindParam(':searchTerm2', $searchTerm2);

// Execute the query
$stmt->execute();

// Fetch and display the results
while ($row = $stmt->fetch()) {
    echo $row['column1'] . ' - ' . $row['column2'] . '<br>';
}
?>