php - 在 Textarea 和 MySQL 上保持换行

标签 php mysql line break

我编写了一些表单以将文本发送到 MySql。我的问题之一是保持换行。在谷歌上找到了一些代码,但我不知道如何使用它。有些尝试并没有真正成功。

我找到的代码是我不知道如何设置的代码。

 function my_nl2br($string){
    $string = str_replace("\n", "<br />", $string);
        if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
            foreach($match as $a){
            foreach($a as $b){
            $string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", "", $b)."</pre>", $string);
            }
        }
    }
return $string;
}

我发现的另一个代码是这个,但如果我尝试从数据库中读取它,换行符将不起作用。

if (isset($_POST['submit'])) 
{ 
    $text = trim($_POST['text']); 
    $text = stripslashes($text); 
    $text = htmlspecialchars($text); 

    echo 'you entered:<br><br>' . nl2br($text); 
} 

这是我要植入的代码。

  if(isset($_POST['title']))
      {
        $title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
        $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
        $applepart = mysql_real_escape_string(htmlspecialchars($_POST['applepart']));
        $partnumber = mysql_real_escape_string(htmlspecialchars($_POST['partnumber']));
        $productcode = mysql_real_escape_string(htmlspecialchars($_POST['productcode']));
        $compatibility = mysql_real_escape_string(htmlspecialchars($_POST['compatibility']));
        $url_bild = mysql_real_escape_string(htmlspecialchars($_POST['url_bild']));
        $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
        $insert = mysql_query("INSERT INTO `adressbuch` (`title`,`description`,`applepart`,`partnumber`,`productcode`,`compatibility`,`url_bild`,`price`) VALUES ('$title','$description','$applepart','$partnumber','$productcode','$compatibility','$url_bild','$price')");
        if (!$insert)
        {
          die('Eintrag konnte nicht gespeichert werden: ' . mysql_error());
        }
      }

    ?>

    <form method="POST" action="?page= ">
      <span>Neuer Eintrag:</span> <br />
      <span>Title</span><input type="text" name="title" /> <br />
      <span>Description</span><textarea cols="16" rows="5"  name="description"></textarea> <br />
      <span>Apple Part</span><input type="text" name="applepart" /> <br />
      <span>Part Number</span><input type="text" name="partnumber" /> <br />
      <span>Product Code</span><input type="text" name="productcode" /> <br />
      <span>Compatibility</span><input type="text" name="compatibility" /> <br />
      <span>Image</span><input type="text" name="url_bild" /> <br />
      <span>Price</span><input type="text" name="price" /> <br />
      <input type="submit" value="Speichern"/> <br />
    </form>

大家加油

最佳答案

要在数据库中存储title,尝试:

$title = real_escape_string(nl2br(htmlspecialchars($_POST['title'])));

关于php - 在 Textarea 和 MySQL 上保持换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11377447/

相关文章:

php - 选择然后更新计数 mysqli

java - JBoss 应用服务器 7.0.1 CRUD 应用程序

r - ggplot 中超过六种形状

php - 错误的 PHP 脚本,帮我减少 28 mysql_queries

php - 重新索引数字数组键

php - 从 php 编辑表格中的字段

python - LINE python库无法登录

php - 我们如何使用数据库动态更改一些下拉列表?

mysql - php删除脚本

JavaFX 获取旋转 imageView 的角坐标?