How to store line breaks from textarea in mySQL database correctly?

I had a problem sending and storing textarea content, for which I could not find the right solution.

I have a form with a textarea field called a "description".

When I submit a form with multi-line text like

Line 1
Line 2

Line 3

It is stored in mySQL db with much newer lines. It becomes: -

Line 1

Line 2


Line 3

when i check db using phpmyadmin.

I tried both VARCHAR and TEXT types, same thing. What do I need to do to keep the contents of textarea accurate?

Corresponding lines codeignign aRow [$ strField] = strip_tags ($ this-> input-> post ($ strField)); $ this-> db-> insert ($ this-> strTable, $ aRow);

$ strField will be my textarea field, and insertion will be a CI function.

+4
source share
3 answers

Ok, I found the answer. I am using CI 2.0 and this is an existing bug that has already been reported. https://bitbucket.org/ellislab/codeigniter/issue/332/newlines-in-textareas-are-duplicated

A quick fix is ​​as follows (look for the file in /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.

+7
source

It seems that a new line is added after each new line. This is due to the way the data is inserted into the database. Want to share this code?

+1
source

try using \ n in the database and when you show the entry use nl2br()

for more

0
source

Source: https://habr.com/ru/post/1342940/


All Articles