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.
LVL 1
Simon336697Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fouriceCommented:
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);
0
fouriceCommented:
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.
0
Simon336697Author Commented:
Hi fourice, mate thank you for your kind help.
I will test once again bud just to make sure and post back results.
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

fouriceCommented:
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.
0
Simon336697Author Commented:
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
0
fouriceCommented:
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.
0
Simon336697Author Commented:
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. ||
0
Simon336697Author Commented:
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.
0
fouriceCommented:
Hmm, perhaps then you can do it the other way around. So store your data in the db with
$bothDescription = mysql_real_escape_string(nl2br($_POST['bothdescriptionarea']));
That way the browser should work fine and for editing use:

function br2nl($string)
{
    return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}
echo br2nl($row['description_ent']);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Simon336697Author Commented:
fourice youre the best, thank you mate.......sorry for the delay getting back to you :>)
Ill play with this thanks fourice :>)
0
Simon336697Author Commented:
Thanks again fourice ... take care mate :>)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.