r/PHPhelp • u/danlindley • Aug 07 '25
Backslashes viewable with php echo
I promise i have read around prior to posting but I i just don't get how to make this work. I've tried reading and experimenting with htmlspecialchars, htmlentities,and mysql_real_escape_string but its not going in and can't figure out to get things "human legible" (i.e. no ampersand and apos or \' )
<?php
/*----------------------- FORM PROCESSING Update casualty details-------------------*/
//Check if the update was submitted
if (isset($_POST['notesupdate'])) {
$notes = $_POST["notes"];
try {
$statement = $conn->prepare("UPDATE tbl_notes
SET
tbl_notes.note = :note
WHERE
note_id=:note_id");
$statement->execute([
'note_id' => $note_id,
'note' => $notes
]);
echo "<script>window.location = window.location</script>";
} catch (PDOException $e) {
echo "Database Error: Could not update the notes.<br>" . $e->getMessage();
exit();
} catch (Exception $e) {
echo "General Error: Could not update the notes.<br>" . $e->getMessage();
exit();
}
}
/*------------ END FORM ----------------*/
?>
<div class="card-header">
<form action="" method="post" id="">
<strong>Notes</strong>
</div>
<div class="card-body">
<div class="row">
<div class="col-sm px-md-5" >
<textarea id="notes" name="notes" rows="40" cols="50">
<?php echo htmlspecialchars($cas_notes); ?></textarea>
<input type="submit" name="notesupdate" value="Save" class="btn btn-success">
</form>
</div>
</div>
</div>I have the LONGTEXT field to store the notes in the database. Each time I submit anything with ' or " it is converted and stored in the database as \' or ' depending on the method used.
Ideally I'd like to be able to store this information "safely" and subsequently return it to the user legibly. I'm not sure why it is different on this field but it isn't playing nice.
Thanks
DAn
View all comments
Show parent comment
1
u/allen_jb Aug 07 '25 edited Aug 07 '25
Switch to the network tab and submit the form - you should see the request appear in the list. If you click on it you'll be able to inspect all the data sent to the server.
You may need to enable the 'preserve log' option to persist entries across redirects / new pages.
This will allow you to verify if the extra quotes and backslashes are being added client-side or server-side.