What are the potential pitfalls of generating and calling links dynamically from form fields in PHP?

Generating and calling links dynamically from form fields in PHP can open up potential security vulnerabilities such as SQL injection or cross-site scripting attacks if user input is not properly sanitized. To mitigate these risks, it is crucial to validate and sanitize user input before using it to generate or call links. This can be done by using functions like htmlspecialchars() to escape special characters and prevent malicious code execution.

// Example of sanitizing user input before generating a link dynamically
$userInput = $_POST['user_input'];
$sanitizedInput = htmlspecialchars($userInput);

$link = "https://example.com/?param=" . $sanitizedInput;

echo "<a href='$link'>Click here</a>";