Unwanted new line characters "rn" appearing on blog posts and in textarea on edit?

Hey everyone,

I was hoping somebody could maybe point me in the right direction here. I’ve made many many simple blogs and have never had this problem so I am totally stumped. Searches of the web and forums haven’t provided me with a solution yet so I was going to run this by yall and see what you think. I’m hoping it’s just something I’ve overlooked cause I’m out of ideas.

The issue is that when I make a post like:


<p>
This is the text of the post
</p>

It displays like this:

rnThis is the text of the postrn

For some reason the new line characters from my entry are being displayed. This is also true when I edit the entry. Instead of the textarea reading the new line characters and keeping my entry formatted properly it is all connected with no line breaks (which makes it inconvenient to edit as the entry becomes one endless line with “rn” where there should simply be a new line. Ill paste what It displays in the textarea in my edit script:


<p>rnThis is the text of the postrn</p>

I have no idea whats going on, like I said this has never happened to me before. Here is my code for the adding, viewing, and editing of the entry:



```php

public function add_blog($title, $body, $image_path) {
        if(!empty($title) && !empty($body)) {
            $blogTitle = trim(mysql_real_escape_string($title));
            $blogBody = trim(mysql_real_escape_string($body));
            if(!empty($image_path)) {
                $image = trim(mysql_real_escape_string($image_path));
                $q = "INSERT INTO blog (blog_id, blog_title, blog_body, blog_image, date_entered)
                      VALUES (0, '$blogTitle', '$blogBody', '$image', NOW())";
            }else{
                $q = "INSERT INTO blog (blog_id, blog_title, blog_body, date_entered)
                      VALUES (0, '$blogTitle', '$blogBody', NOW())";
            }
            $r = mysql_query($q);
            if($r) {
                $this-&gt;_messages[] = "&lt;p&gt;Your entry '" . stripslashes($blogTitle) . "' was successful.&lt;/p&gt;";
            }else{
                $this-&gt;_messages[] = '&lt;p class="error"&gt;Could not add your entry.&lt;/p&gt;';
                if(is_admin()) {
                    $this-&gt;_messages[] = '&lt;p class="error"&gt;' . mysql_error() . '&lt;/p&gt;';
                }
            }
        }
    } // END add_blog

public function edit_blog($id, $title, $body, $image_path) {
        if(!empty($title) && !empty($body)) {
            $blogTitle = trim(mysql_real_escape_string($title));
            $blogBody = trim(mysql_real_escape_string($body));
            if(!empty($image_path)) {
                $image_path = trim(mysql_real_escape_string($image_path));
                $q = "UPDATE blog SET blog_title='$blogTitle', blog_body='$blogBody',
                      blog_image='$image_path' WHERE blog_id=$id";
            }else{
                $q = "UPDATE blog SET blog_title='$blogTitle', blog_body='$blogBody'
                      WHERE blog_id=$id";
            }
            $r = mysql_query($q);
            if($r) {
                $this-&gt;_messages[] = "&lt;p&gt;Your entry '" . stripslashes($blogTitle) . "' was successfully updated.&lt;/p&gt;";
            }else{
                $this-&gt;_messages[] = '&lt;p class="error"&gt;Could not update your entry.&lt;/p&gt;';
                if($this-&gt;is_admin()) {
                    $this-&gt;_messages[] = '&lt;p class="error"&gt;' . mysql_error() . '&lt;/p&gt;';
                }
            }
        }
    } // END edit_blog

public function get_blog($id) {
        if(is_numeric($id)) {
            $q = "SELECT *, DATE_FORMAT(date_entered, '%M %D, %Y @ %r') AS dr FROM blog WHERE blog_id=$id";
            $r = mysql_query($q);
            if($r) {
                $row = mysql_fetch_array($r);
                $title = trim(stripslashes($row['blog_title']));
                $text = trim(stripslashes($row['blog_body']));
                $image = trim($row['blog_image']);
                $date = trim($row['dr']);
                // Create array of blog values to be returned
                $entry = array('id' =&gt; $id, 'title' =&gt; $title, 'text' =&gt; $text,
                               '$image' =&gt; $image, 'date' =&gt; $date);
                return $entry;
            }else{
                $this-&gt;_messages[] = "Could not access blog.";
                return false;
            }
        }else{
            $this-&gt;_messages[] = "Invalid blog ID.";
            return false;
        }
    } // END get_blog

    public function show_blog($id) {
        // use get_blog to access content then display
        if($entry = $this-&gt;get_blog($id)) {
            echo '&lt;center&gt;';
            if(!empty($entry['image'])) {
                echo '&lt;img height="250" src="' . $entry['image'] . '" /&gt;';
            }
            echo '&lt;h1&gt;' . $entry['title'] . '&lt;/h1&gt;
                    ' . $entry['date'] . '
                    &lt;/center&gt;
                    &lt;br /&gt;
                    ' . $entry['text'] . '
                    &lt;br /&gt;
                    &lt;br /&gt;';
            if($this-&gt;is_auth()) {
                echo '&lt;div style="clear:both; margin-top:10px; margin-bottom:10px;"&gt;
                      &lt;center&gt;
                      &lt;a href="editBlog.php?blogID=' . $id . '" class="mainButton"&gt;Edit&lt;/a&gt;
                      &lt;a href="deleteBlog.php?blogID=' . $id . '" class="mainButton"&gt;Delete&lt;/a&gt;
                      &lt;/center&gt;
                      &lt;/div&gt;
                      &lt;div class="clr"&gt; &lt;/div&gt;';
            }
        }
    } // END show_blog



If anyone see's what I'm doing wrong I would (and so would my brain!) greatly appreciate the insight. Thank you guys/gals.

P.S. If I messed up the code/highlight system on this post I apologize and will try to get it fixed right away.

It’s got to be something to do with the method of making the strings safe. I’m not familiar with the older mysql functions as I moved to PDO, but the manual page mentions checking whether magic_quotes_gpc is enabled, you should first call stripslashes() to make sure the special characters aren’t escaped twice - I see you call that in get_blog() but not when you’re storing it.

I will check that out and do some experimentation. It must be something like that as you said. Ok, I’ll post any progress in this thread as it occurs. Thanks a ton for looking into it, I really do appreciate it. And so does the hair on my head that I haven’t pulled out lol.

Another way to narrow it down quickly,
remove all extras from


$blogBody = trim(mysql_real_escape_string($body));
// becomes
$blogBody =$body;
// and
$text = trim(stripslashes($row['blog_body']));
// becomes
$text =$row['blog_body'];

You should see right away when the problem goes away.

Ok, I did get it figured out and lorenw that is exactly what I needed to do. Turns out I was using mysql_real_escape_string twice and that’s what was interfering.

On my object (my blog class) the add_blog method escaped the string, AND on my form processing script that retrieved the $_POST data. So, it was escaped as the $_POST info was received AND when the object method took over. By the time it was being inserted into the database the damage was done. So, it all works great now thank you two so much. I was starting to get really peeved lol.