php mysql存储数据库中文本区域的换行符

时间:2012-10-04 14:28:42

标签: php mysql database format

我有一个输入textarea,我想存储用户在数据库的文本区域中的换行符,以便它以与输入文本相同的方式回显输出文本

例如:

Some text some text some text some text


new line some text some text new line 

new line some text new line some text

这是我目前的上传脚本:

$sql = "insert into people (price, contact, category, username, fname, lname, expire, filename) values (:price, :contact, :category, :user_id, :fname, :lname, now() + INTERVAL 1 MONTH, :imagename)";
$q = $conn->prepare($sql) or die("failed!");
$q->bindParam(':price', $price, PDO::PARAM_STR);
$q->bindParam(':contact', $contact, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$q->bindParam(':fname', $fname, PDO::PARAM_STR);
$q->bindParam(':lname', $lname, PDO::PARAM_STR);
$q->bindParam(':imagename', $imagename, PDO::PARAM_STR);
$q->execute();

4 个答案:

答案 0 :(得分:24)

文本区域中的换行符是换行符,例如\n。它们不是以HTML格式呈现的,因此如果您只是回显输出,它们就不会显示出来。您可以在<textarea><pre>标记内部回显,也可以使用nl2br()将新行替换为<br>标记,以便它可以显示为HTML。< / p>

答案 1 :(得分:2)

您可以使用mysql_real_escape_string

在存储之前转义换行符

此处的文档:http://php.net/manual/en/function.mysql-real-escape-string.php

答案 2 :(得分:0)

您还可以使用javascript通过使用替换

将文本分解为\n to <br>
str.replace("\n", "<br />","g");

答案 3 :(得分:0)

我遇到了同样的问题,现在正在运作:

$query = "UPDATE your_table 
         SET your_text_field = 'line 1'" . '\n' . "'line2' 
         WHERE your_id_field = " . $id . "';";