简体   繁体   English

如何在mySQL数据库中正确存储textarea的换行符?

[英]How do I store line-breaks from textarea properly in mySQL database?

I've been having this issue with submitting and storing textarea content, to which I've been unable to find a proper solution. 我一直有提交和存储textarea内容的问题,我一直无法找到合适的解决方案。

I have a form, with a textarea field, called "description". 我有一个表单,带有textarea字段,称为“描述”。

When I submit the form with multiline text, like 当我提交带有多行文字的表格时,如

Line 1 第1行
Line 2 第2行

Line 3 第3行

It gets stored in the mySQL db with much more new lines. 它存储在mySQL数据库中,包含更多新行。 It becomes :- 它成为了 :-

Line 1 第1行

Line 2 第2行


Line 3 第3行

when I check the db with phpmyadmin. 当我用phpmyadmin检查数据库时。

I've tried both VARCHAR and TEXT types, its the same. 我已经尝试过VARCHAR和TEXT类型,它们都是一样的。 What do I need to do to store the textarea content accurately? 我需要做些什么来准确存储textarea内容?

The relevant codeigniter lines are aRow[$strField] = strip_tags($this->input->post($strField)); 相关的codeigniter行是aRow [$ strField] = strip_tags($ this-> input-> post($ strField)); $this->db->insert($this->strTable, $aRow); $ this-> db-> insert($ this-> strTable,$ aRow);

$strField will be my textarea field, and the insert is a CI function. $ strField将是我的textarea字段,插入是CI函数。

Okay I've found the answer. 好的,我找到了答案。 I'm using CI 2.0, and it is an existing bug that has already been reported. 我正在使用CI 2.0,这是一个已经报告的现有错误。 https://bitbucket.org/ellislab/codeigniter/issue/332/newlines-in-textareas-are-duplicated https://bitbucket.org/ellislab/codeigniter/issue/332/newlines-in-textareas-are-duplicated

The quick fix is as below (look for the file in /system/core/Input.php) 快速修复如下(在/system/core/Input.php中查找文件)

//$str = str_replace(array( "\r\n", "\r"), PHP_EOL, $str);
$str = preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);

Hope this helps someone out there. 希望这有助于那里的人。

It seems like a newline is being added after each new line. 似乎在每个新行之后添加换行符。 This has to do with how the data is inserted into the database. 这与数据如何插入数据库有关。 Care to share that code? 小心分享那段代码?

try to use \\n in database and when you showing record use nl2br() 尝试在数据库中使用\\ n并在显示记录时使用nl2br()

for more 更多

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

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