How can SQL injection vulnerabilities be addressed in PHP scripts like the ones discussed in the forum thread?

SQL injection vulnerabilities in PHP scripts can be addressed by using prepared statements with parameterized queries instead of directly concatenating user input into SQL queries. This helps to prevent malicious SQL code from being injected into the query and executed by the database. Example PHP code snippet using prepared statements to address SQL injection vulnerabilities:

<?php
// Establish a database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Use prepared statements to prevent SQL injection
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

// Set parameters and execute
$username = $_POST['username'];
$stmt->execute();

// Process results
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // Output user data
    echo "Username: " . $row['username'] . "<br>";
    echo "Email: " . $row['email'] . "<br>";
}

// Close statement and connection
$stmt->close();
$conn->close();
?>