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
7
u/allen_jb Aug 07 '25
You shouldn't be seeing extra backslashes in the database records themselves. This sounds like the code is double-escaping, or incorrectly escaping, data before it's put into the database.
From the code you've posted, you're using prepared statements, so there's no need to run data through
mysqli_real_escape_string()and similar DB escaping functions. Prepared statements handled escaping for you.Not seen so much in recent code, but it used to be common to escape any data coming in via $_POST or $_GET. PHP used to have a feature related to this called "magic quotes". This feature was removed long ago but many people decided to emulate this and worse.
If you have any code anywhere that's using
addslashes(), remove it (and consider what the correct escaping, if any, should be, based on the context).addslashes()is almost never the correct function for escaping data and any code that uses it is highly suspect.I would recommend not escaping data for HTML before putting it into the database. You should escape data based on what you're currently outputting it to. ie. only escape data for HTML as you're putting it into the views / templates. Keep the original data in the database. (Additionally this avoids issues with putting data into non-HTML formats such as plain text emails or CSV)
2
u/colshrapnel Aug 07 '25
You are getting $conn variable from somewhere. Probably an included file. You may either post this file's contents here or just look closely at the code in it. I bet you will find the ruffian there.
1
u/colshrapnel Aug 07 '25
What is your PHP version?
3
u/MateusAzevedo Aug 07 '25 edited Aug 07 '25
Just a shot in the dark: open
php.ini, find the "auto_prepend_file" setting and check if something is configured there. I know, it's very unlikely, but this is the only thing I know that could automatically "do something" before your code runs.After that, you'll need to do some debugging. I'd start with:
1- Open the page, right click -> show page source (
ctrl + u). Find the textarea input and see the raw data browser got from PHP.2- Just after
if (isset($_POST['notesupdate'])) {, add:<pre>tag is important to see the raw data, otherwise the browser will render HTML entities. You can ignore the tags, but then you must instead view the page source to se the raw data, not what's displayed.These 2 step will help identify in which step
\is added, when displaying data or when receiving/storing it.2
u/equilni Aug 07 '25
Where is the rest of the code? You are just showing the update here. $cas_notes is coming from somewhere…
1
u/Big-Dragonfly-3700 Aug 07 '25
In addition to whatever is causing the main problem, the above is adding a new-line character and spaces/tabs to the start of whatever you are echoing out in that textarea, and since you are not trimming the input data before validating and using it, these additional characters are being UPDATED into the column in the database, every time you perform this operation.
You need to move the opening <?php tag to be immediately after the cols="50"> and you need to trim() input data in your form processing code before validating and using it.
1
u/equilni Aug 08 '25 edited Aug 08 '25
Making this easy for you OP.
Test exactly this in isolation. Just the 2/3 “pages” of PHP and HTML - NO Javascript.
Fix the connection as noted before, then test again.
If you are not exhibiting the same issue then the issue is not here and somewhere else.
I suspect this as the HTML is missing elements and the JS is oddly noted here but doesn’t correspond with what’s shown (ie not showing the full code). Select2 is a jquery select box library (https://github.com/select2/select2) so I am suspecting there is more JS going on here…