My site has a form with a comment field. If someone enters, for example:
"What" is 'up'?>
into the comment field, it gets inserted into mysql using PDO prepared statement and subsequently appears in the database exactly like this:
\"what\" is \'up\'?>
Then when I pull that data back out of the database, I use:
$comment=htmlspecialchars($row['comment']);
But when I output it to the page using "echo $comment", it gets outputted to the page as:
\"what\" is \'up\'?>
Shouldn't htmlspecialchars remove those slashes?
FWIW, before converting to PDO, I used to use mysqli_real_escape_string()
before inserting and then htmlspecialchars()
before displaying.
Insert code:
$comment=$_POST['comment'];
$stmt = $pdo->prepare("INSERT into details (firstname, lastname, comment) values (:firstname, :lastname, :comment)");
$stmt->execute([':firstname' => $firstname, ':lastname' => $lastname, ':comment' => $comment]);