SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Location
    Colorado
    Posts
    20
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question 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:
    Code:
    <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:

    Code:
    <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:

    Code:
    Code 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->_messages[] = "<p>Your entry '" . stripslashes($blogTitle) . "' was successful.</p>";
                }else{
                    $this->_messages[] = '<p class="error">Could not add your entry.</p>';
                    if(is_admin()) {
                        $this->_messages[] = '<p class="error">' . mysql_error() . '</p>';
                    }
                }   
            }
        } // 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->_messages[] = "<p>Your entry '" . stripslashes($blogTitle) . "' was successfully updated.</p>";
                }else{
                    $this->_messages[] = '<p class="error">Could not update your entry.</p>';
                    if($this->is_admin()) {
                        $this->_messages[] = '<p class="error">' . mysql_error() . '</p>';
                    }
                }   
            }
        } // 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' => $id, 'title' => $title, 'text' => $text,
                                   '$image' => $image, 'date' => $date);
                    return $entry;
                }else{
                    $this->_messages[] = "Could not access blog.";
                    return false;
                }
            }else{
                $this->_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->get_blog($id)) {
                echo '<center>';
                if(!empty($entry['image'])) {
                    echo '<img height="250" src="' . $entry['image'] . '" />';
                }      
                echo '<h1>' . $entry['title'] . '</h1>
                        ' . $entry['date'] . '
                        </center>
                        <br />
                        ' . $entry['text'] . '
                        <br />
                        <br />';
                if($this->is_auth()) {
                    echo '<div style="clear:both; margin-top:10px; margin-bottom:10px;">
                          <center>
                          <a href="editBlog.php?blogID=' . $id . '" class="mainButton">Edit</a> 
                          <a href="deleteBlog.php?blogID=' . $id . '" class="mainButton">Delete</a>
                          </center>
                          </div>
                          <div class="clr"> </div>';
                }
            }
        } // 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.

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    413
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    SitePoint Member
    Join Date
    Oct 2013
    Location
    Colorado
    Posts
    20
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,100
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Another way to narrow it down quickly,
    remove all extras from
    PHP Code:
    $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.
    What I lack in acuracy I make up for in misteaks

  5. #5
    SitePoint Member
    Join Date
    Oct 2013
    Location
    Colorado
    Posts
    20
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •