• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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.
0
Simon336697
Asked:
Simon336697
  • 6
  • 5
1 Solution
 
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now