Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Displaying output when editing gives be <br /> tags

Hi everyone.
Guys, on inserting records into a mysql database, im using the following..

$bothDescription = mysql_real_escape_string(nl2br($_POST['bothdescriptionarea']));

So, let's say, i insert the following...

Hi
this is line 2
this is line 3

On display in a browser, I get the following which is good.

Hi
this is line 2
this is line 3

But, on editing a record, Im getting the following.....

Hi<br />
this is line 2<br />
this is line 3      

Im not sure what im doing wrong here.

On INSERTING RECORDS, im using
mysql_real_escape_string
AND
nl2br  (because I want to preserve the way i have typed in including any carriage returns)

It is just the edit part im having the <br> tags showing, which I dont want to show when i am editng a record.

I know you guys have advised me to use mysql_real_escape_string when inserting records, which I am doing.
But using mysql_real_escape_string alone does not preserve any carriage returns or line breaks you insert, which is why i am also using nl2br with mysql_real_escape_string.

Any help greatly appreciated.
Avatar of fourice
fourice
Flag of Netherlands image

You might want to try to remove the nl2br before saving it into the db and use it when you display it in your browser:
So:
$bothDescription = mysql_real_escape_string($_POST['bothdescriptionarea']);
and for output use:
echo nl2br($variableName);
Sorry, didn't read this:
" know you guys have advised me to use mysql_real_escape_string when inserting records, which I am doing.
But using mysql_real_escape_string alone does not preserve any carriage returns or line breaks you insert, which is why i am also using nl2br with mysql_real_escape_string.
"
But I do it like I said myself and that normally works perfect, not using mysql_real_escape_string btw.
Avatar of Simon336697

ASKER

Hi fourice, mate thank you for your kind help.
I will test once again bud just to make sure and post back results.
Ok, but keep in mind that you may never store a variable in the db right away (without some proper checks). Reading my post perhaps gives you that idea.
The idea is that you can store carriage returns without nl2br. For ouput to the browser you can use that function and for filling your textarea (with the newlines) you can just use the stored data.
fourice,
If id just use mysql_real_escape_string for insert,

This is line 1
This is line 2
This is line 3
This is line 4

with

$bothDescription = mysql_real_escape_string($_POST['bothdescriptionarea']);


On viewing the record in the database, it is seen as follows..

Please see the file attached.

How would I display this in a browser with new lines preserved?
as-seen-from-mysql.jpg
You should be able to do something like:
$strQuery = "SELECT * FROM entries_ent WHERE name_ent='Go figure'";
$result = mysql_query($strQuery);
$row = mysql_fetch_array($result);
echo nl2br($row['description_ent']);
Typed it out of my head, so I hope it's ok.
Mate thanks for your help on this.

I tried that but that does not work, and I think it is because there has to be \n tags already in the database, which in the printscreen I posted, there is none.
There are only paragraph tags eg. ||
If i insert a record like below...

Line 1<br>
Line 2

With your method, it does work....eg. output is:

Line 1

Line 2

But I dont want to have to type <br> tags when inserting records, especially in a textarea where I might want to cut and paste whole blocks of text with new lines, paragraphs, brs already there, and want to preserve all this formatting on output.
ASKER CERTIFIED SOLUTION
Avatar of fourice
fourice
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
fourice youre the best, thank you mate.......sorry for the delay getting back to you :>)
Ill play with this thanks fourice :>)
Thanks again fourice ... take care mate :>)