简体   繁体   English

我如何从 <textarea> 使用PHP

[英]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. 首先如何获取\\n<br>输入mysql。

  2. Why doesnt echo nl2br($row->comment); 为什么不echo nl2br($row->comment); work? 工作? I put \\n 's into my database manually and it didn't output them. 我将\\n手动放入数据库中,但没有输出。 Testing\\nTesting\\nTesting came out as "Testing\\nTesting\\nTesting" . Testing\\nTesting\\nTesting作为"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. 编辑:我知道有很多关于stackoverflow的内容,但它们似乎都无法解释整个过程,我正努力将其拼凑在一起。

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 1)如何获得\\ n或
to input into mysql in the first place. 首先输入mysql。

Use the blob type. 使用blob类型。

2) Why doesnt echo nl2br($row->comment); 2)为什么不回显nl2br($ row-> comment); work? 工作? I put \\n's into my database manually and it didn't output them. 我将\\ n手动放入数据库中,但没有输出。 Testing\\nTesting\\nTesting came out as "Testing\\nTesting\\nTesting". Testing \\ nTesting \\ nTesting的名称为“ Testing \\ nTesting \\ nTesting”。

It should work as per the documentation: https://secure.php.net/manual/en/function.nl2br.php 它应该按照文档工作: https : //secure.php.net/manual/en/function.nl2br.php

If it does not, please provide a PHP code sample demonstrating the issue 如果没有,请提供一个PHP代码示例来演示问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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