简体   繁体   中英

How can I store paragraphs in mysql from a <textarea> using php

Could someone please help me import and display paragraphs in my text input?

My problems are...

  1. How can I get a \\n or <br> to input into mysql in the first place.

  2. Why doesnt echo nl2br($row->comment); work? I put \\n 's into my database manually and it didn't output them. Testing\\nTesting\\nTesting came out as "Testing\\nTesting\\nTesting" .

Thanks in advance for any help.

EDIT: I know there are lots of these on stackoverflow but none of them seem to explain the whole process and i'm struggling to piece it together.

Code:

if(isset($_POST['btn_wall'])){

  $sUsername = safeString($_SESSION['username']);
  $sWall = safeString($_POST['post_wall']);

  if($sWall != ""){
    $query = "INSERT INTO wall (user, comment, dt) VALUES (:user, :comment, now())";
    $stmt = $pdo->prepare($query);
    $stmt->bindParam(':user', $sUsername, PDO::PARAM_STR);
    $stmt->bindParam(':comment', $sWall, PDO::PARAM_STR);
    $stmt->execute();
  }
}

I then recreate it with:

<ul>
    <?php
    $query = "SELECT user, comment, dt FROM wall ORDER BY dt DESC LIMIT 6";
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    $count = $stmt->rowCount();

    if($count > 0){
      while($row = $stmt->fetchObject()){
        echo "<li>".nl2br($row->comment)." - ".$row->user."</li>";
      }
    }
    ?>
</ul>

1) How can I get a \\n or
to input into mysql in the first place.

Use the blob type.

2) Why doesnt echo nl2br($row->comment); work? I put \\n's into my database manually and it didn't output them. Testing\\nTesting\\nTesting came out as "Testing\\nTesting\\nTesting".

It should work as per the documentation: https://secure.php.net/manual/en/function.nl2br.php

If it does not, please provide a PHP code sample demonstrating the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM