What are some potential pitfalls when manipulating text strings in PHP?
One potential pitfall when manipulating text strings in PHP is not properly escaping special characters, which can lead to security vulnerabilities such as SQL injection attacks. To solve this issue, always use functions like `mysqli_real_escape_string()` or prepared statements when interacting with databases to prevent malicious input.
// Example of using prepared statements to prevent SQL injection
$stmt = $mysqli->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $password);
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$password = mysqli_real_escape_string($mysqli, $_POST['password']);
$stmt->execute();
$stmt->close();