What are the potential pitfalls of using static values in PHP queries, and how can they be avoided?

Using static values in PHP queries can make your code vulnerable to SQL injection attacks. To avoid this, you should always use prepared statements with parameterized queries. This way, the user input is treated as data rather than executable code, preventing malicious SQL injection attempts.

// Avoid using static values in queries
$unsafe_variable = $_POST['input'];
$query = "SELECT * FROM users WHERE username = '$unsafe_variable'";
$result = mysqli_query($connection, $query);

// Use prepared statements to avoid SQL injection
$safe_variable = $_POST['input'];
$stmt = $connection->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $safe_variable);
$stmt->execute();
$result = $stmt->get_result();