Saving Special Characters along with Line Breaks
Results 1 to 2 of 2

Thread: Saving Special Characters along with Line Breaks

  1. #1
    Member
    Join Date
    Jan 2010
    Posts
    47

    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
    Last edited by rashid100; 11-24-2012 at 11:34 AM.

  2. #2
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    A few things:

    1. Storing data in a database does not "remove" line breaks. The line breaks are most likely still there - however, line breaks are ignored in HTML. If this is the case, you need to replace them with the <br> element.

    2. You should not be using the mysql_* functions. They are long outdated, and the deprecation process has begun. You should use mysqli or PDO instead - read more about choosing an API.

    3. You should not be using htmlentities() when storing your data in the database. You should preserve the actual data in the database, and use htmlentities when you display the information on your webpage instead.

    (This will also solve the problem of having to use html_entity_decode() and stripslashes() when you retrieve the data.)

    (As a side note, if you are using htmlentities as a means of preserving "special characters," you need to be using the proper character set instead. UTF-8 is almost always the best choice. Make sure your webpage, scripts, database, and database connection are set up to use UTF-8.)

    4. Your query:
    PHP Code:
    $query=mysql_query(insert into special values($data));
    #  this will cause a parse error. your query needs to be quoted.
    #  you don't specify which columns you are inserting into.
    ##  if there is more than one column, this will cause a mysql error.
    ##  if there is only one column, your database needs to be better designed.
    #  inside your query, you don't quote the $data you are inserting.  this will cause a mysql error.

    # should be:
    $query "INSERT INTO `special` ( `col_name` ) VALUES ( '$data' )"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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