How can SQL injection vulnerabilities be addressed in the PHP code for user registration?

SQL injection vulnerabilities in PHP code for user registration can be addressed by using prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This helps to prevent malicious SQL commands from being executed by escaping special characters and treating user input as data rather than executable code.

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

// Prepare a SQL statement with placeholders
$stmt = $pdo->prepare('INSERT INTO users (username, password) VALUES (:username, :password)');

// Bind parameters to the placeholders
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', $_POST['password']);

// Execute the prepared statement
$stmt->execute();