#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Saving Special Characters along with Line Breaks


    Hi

    I am using the php to inserting the records in mysql table. My table has following feilds

    id int auto increament
    data longtext

    via php i am inserting the record

    PHP Code:
    $data         = isset($_REQUEST['data'])?$_REQUEST['data']:""

       if (isset(
    $_REQUEST['submit']))       
       { 
        
          
    $data=htmlentities(mysql_real_escape_string($data));     
          
    $query=mysql_query(insert into special values($data)); 


    and when I echo this record after retrieving from table its shows me in Single Line whatever user input in multiple lines or in a paragraph with some special characters also.

    PHP Code:
           $result1=mysql_query("select * from special"); 
            if (
    mysql_num_rows($result1)){ 

                         while(
    $row=mysql_fetch_array($result1)){ 

                    
    $str=$row['data']; 
      
      
             
    $str=html_entity_decode(stripslashes($str)); 

             echo 
    "<textarea>".$str."</textarea>"
             } 


              
              
             } 
    I want to show in a original data whats the user actually insert.Can any body help me in this matter.

    Thanks
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    Don't call htmlentities here:
    Code:
    $data=htmlentities(mysql_real_escape_string($data));
    Don't call html_entity_decode or stripslashes here:
    Code:
    $str=html_entity_decode(stripslashes($str));
    DO call htmlentities here:
    Code:
    echo "<textarea>" . htmlentities($str, ENT_QUOTES, 'UTF-8') . "</textarea>";
    When you output newlines into a textarea they should be preserved. If you have a string that you want to show elsewhere in HTML and you want to preserve newlines in it, then you need to convert them into <br />'s. You can do this easily using nl2br().
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0
    Thanks for your reply Now I tried to use this codes as below
    for Inserting
    PHP Code:
          $data=mysql_real_escape_string($data); 
    and for view as the same like
    PHP Code:
    echo "<textarea cols='50' rows='10'>" htmlentities($strENT_QUOTES'UTF-8') . "</textarea>"
    but unfortunately the result is the same let suppose If I enter this type of insertion in different lines:

    Hello how are you
    this "is not" and how much
    you get' and own

    The result is showed in single line:
    Hello how are you\r\nthis \\is not\\ and how much\r\nyou get\' and own

    Hope you understand my point

IMN logo majestic logo threadwatch logo seochat tools logo