\r\n db saving and echo issue

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

kumarbarun89
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Mar 02, 2013 12:50 am

\r\n db saving and echo issue

Postby kumarbarun89 » Wed May 22, 2013 11:18 pm

My problem is that when I save data(large data) from textarea into MySQL db by using $mysqli->real_escape_string($content);, it stores special chars escaped and also \r\n. But when I update the same the same way it does not do that(it is desired).

Second problem is that of the same data being echoed with \r\n on the browser, even nl2br() does not work. I know that even without nl2br(), echo prints data without \r\n except that we do not get <br/>(line breaks).

Below are the codes I've written:

Image

Below code retrieves data to forms if already saved. You can see the problem in above figure.

Code: Select all



<?php
 session_start();
 if((!isset($_SESSION['role'])) && ($_SESSION['role']!='admin')):       
            header("location: index.php"); 
  else:       
?>

<!doctype html>
<html>
<head>
   <title>Chairperson</title>

        <link type="text/css" rel="stylesheet" href="css/theme.css"/>
       
        <meta name="author" content="Barun Kumar"/>
        <meta name="revised" content="04-May-2013"/>
       
       <?php
        include_once 'includes/links.htm';
       ?>
       
</head>
<body>
<?php
include_once 'includes/header.htm';
?>
<div id="main">
    <div id="content" class="fixedncenter">
       
        <div>
          You are here: Admin Panel >> <a href="school_site.php">Universal School</a> >> Chairperson
        </div>
   
        <?php
        /***************Retrieve content**************/
        include 'includes/connection.php';
       
        $query="select * from chairperson";
       
        if ($stmt = $mysqli->prepare($query)) {
         /* execute statement */
    $stmt->execute();
    /* bind result variables */
    $stmt->bind_result($out_id, $out_title, $out_content);
    /* fetch values  */
   
       
   while ($stmt->fetch()) {
        $id=$out_id;
        $title=$out_title;
        $content=$out_content;
    }
     
      /* close statement */
   
    $stmt->close();
}
   
   
 

/* close connection */
$mysqli->close();
        ?>
       
 
       
       
        <!--***************Content Management************************-->
       
        <form action="save_chairperson_page_content.php" method="post">
            <p>
                <label for="title">Title:</label><br/>
                <input type="text" name="title" id="title" style="width:734px;" value="<?php if(!empty($title)){echo $title;} ?>"/>
            </p>
            <p>
                <label for="content">Content:</label><br/>
                <textarea name="content1" id="content1" style="width:734px; height:500px;"/>

                </textarea>
            </p>
            <input type="hidden" name="id" value="<?php if(!empty($id)){ echo $id;}?>"/>
        </form>
       
        <script type="text/javascript">     
           
            document.getElementById("content1").value=<?php if(!empty($content)){ echo json_encode($content); }?>;
            //tinyMCE.activeEditor.setContent('str');
        </script>
    </div><!--fixedncenter-->
</div> <!-- end of main-->


<?php
    include_once 'includes/footer.htm';
?>

</body>
</html>



<?php
endif;   
?>



Below code saves or updates the table:

Code: Select all

<!-- Save Chair Person Page content -->
<!doctype html>
<html>
<head>
   <title>Save Content</title>

        <link type="text/css" rel="stylesheet" href="css/theme.css"/>
       
        <meta name="author" content="Barun Kumar"/>
        <meta name="revised" content="04-May-2013"/>
       
        <meta name="keywords" content="Website Management Software"/>
        <meta name="description" content="Website Management Software Written by Barun Kumar"/>                 
</head>
<body>
<?php
    include_once 'includes/header.htm';
?>
<div id="main">
    <div id="content" class="fixedncenter">
       
       <?php
        include_once 'includes/connection.php';
       
        $id=$_POST['id'];
        $title=$_POST['title'];
        $content=$_POST['content1'];
       
        $e_id=$mysqli->real_escape_string($id);
        $e_title=$mysqli->real_escape_string($title);
        $e_content=$mysqli->real_escape_string($content);
       
        if(empty($_POST['id'])){
           /*********Insert*****************/
            /* Prepared statement, stage 1: prepare */
        if (!($stmt = $mysqli->prepare("insert into chairperson(id, title, content) values(?, ?, ?)"))) {
            echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
            exit();
        }

/* Prepared statement, stage 2: bind and execute */

if (!$stmt->bind_param('iss', $e_id, $e_title, $e_content)) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    exit();
}

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    exit();
}

echo 'Saved successfully. Go <a href="chairperson.php">back</a>';
/* explicit close recommended */
$stmt->close();


        }else{
            /**********Update ***************/
             if (!($stmt = $mysqli->prepare("update chairperson set title='$e_title', content='$e_content' where id=$e_id"))) {
                    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
                    exit();
             }
             
             if (!$stmt->execute()){
                     echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
                     exit();
             }
             
            echo 'updated successfully. Go <a href="chairperson.php">back</a>';
            /* explicit close recommended */
            $stmt->close();
        }
       
       
     /**Close connection **/
       $mysqli->close();         
       
            ?>
   
    </div><!--fixedncenter-->
</div> <!-- end of main-->


<?php
        include_once 'includes/footer.htm';
?>

</body>
</html>

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: \r\n db saving and echo issue

Postby johnj » Sat May 25, 2013 1:10 am

if you do not want \r\n, just remove it before you save and retrieve data from the database.

if nl2br does not work then try using str_replace or preg_replace.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron