Quick and simple (I hope) question; If a user inputs new lines in a text field, ie:
43 Dennis
Beeston
How can I save the new line rather than have it transferred to the mysql server as one line?!
I am using PHP and mysql.
When you output the field from the database to a html document, either use <pre> or nl2br() (or a <textarea>).
To save the newlines in the database, take a look at mysql_real_escape_string
If you want your newlines displayed as breaks on the page when you display the records, use nl2br.
MySQL should preserve the newline - could it be your output (or your mysql gui) that isn't displaying it?
Related
I'm using TinyMCE to save some HTML into an SQL table in phpMyAdmin. Inserting and retrieving the row from the table works fine.
I'm using a regex to translate some short codes in the retrieved text and this is where the problem arises.
This is my regular expression, which simply gets the text between two short codes with possible html tags and new lines:
/(<.+>)?[[]{$code}[]](<\/.+>)?((?:\n.+\n?)+)(<.+>)?[[]{$code}[]](<\/.+>)?/
When I retrieve the HTML from the DB and run the regex on it, the preg_match_all() fails to match anything, but when I double-click on the row in the database and open the in-line editor, phpMyAdmin does...something and performs an update on the row automatically and sets the text to a new value; Then, when I run the regex on the newly updated value, preg_match_all() matches the correct values.
I was thinking it was some automatic text encoding conversion or something, but running mb_detect_encoding() on the HTML before I insert it indeed confirms that the encoding is UTF-8 same as the table's utf8_unicode_ci.
I then compared the text plus EOL characters before and after the update in Notepad++ and they're exactly the same, yet my regex doesn't work before phpMyAdmin updates it.
What is phpMyAdmin doing to fix the text and how can I do it before it gets inserted in to the database? Why is it automatically updating the row at all?
I added some more code to the regular expression to check for content after the short code on the same line and now preg_match_all() matches correctly every time. I'm still not sure what's going on there as the content before and after the update are identical in every test I've tried (Same text, same amount of spaces and new line characters).
Regardless, I fixed it by adding the below regex after the check for the end HTML tag:
(?:.+)?
So the full expression is:
(<.+>)?[[]{$code}[]](<\/.+>)?(?:.+)?((?:\n.+\n?)+)(<.+>)?[[]{$code}[]](<\/.+>)?
i wonder how to store the values passed by html form as it is interms of format into mysql table. can anyone help me.
let me clear my question.
when the user enter his address in the text area like,
no:14,cross cut road,
XXX nagar,
Tamilnadu,
india.
and when this value is passed from a form and stored in mysql table, it'll displayed in a single line when i retrieved it in the next time, like
no:14,cross cut road,XXX nagar,Tamilnadu,india.
but i want to display it like the previous one like how they enter, in the same format.
can anyone help how to do this???
thanks in advance.
i've no idea to use any web editors. so i tried n2lbr function and got the same format. New line problem solved but now am facing the space problem. if i leave 10 spaces and wrote a text, it'll display the text only.. not the spaces. how to solve this??
You should use a wysiwyg editor like ckeditor.
Use
mysql_real_escape_string doesn't remove line breaks, it escapes them.
While displaying use
nl2br for adding those line breaks.
when you press enter in the text area the '\n' character is appended to your text and this character is stored in DB but when you read from the DB and want to visualize your text, you should consider that your component recognize the '\n' character, if it doesn't recognize newline character (i.e. '\n') then you should replace '\n' with html break (i.e. br) tag to show the new line in html.
I have a form with a textarea whose results are inserted into a mysql database. I'm using nl2br to preserve the line breaks.
However, because this inserts br's in the text, when a user goes to edit what they've entered in the textarea, it shows all the br's in the textarea which were saved in mysql (looks ugly for people who don't know html).
So, if I don't use nl2br, the line breaks look nice when echoed back in the textarea but not saved in the database correctly. If I use nl2br, the line breaks are saved in the database correctly but look ugly when echoed back. How do I echo the saved textarea contents back onto the page without showing the br's all over the place?
Store the test in the database with break lines and when you echo it out to the screen run it through nl2br.
HI,
I am creating on comments form where users will be commented and will be stored in the MYSQL database. The problem what I am facing is, it is stored as the single line in the database. It should be stored with exact format how user is entered in the form(like new lines and everything). I am using PHP to store it in the MySQL db.
First store it as text or longtext. Second, when showing the comment, use a function like nl2br to convert newlines to html <br> elements. This way, linebreaks are preserved.
Your text is stored just fine in the database if you are putting it into a long enough text-type field (e.g. TEXT), including the newlines in the user input.
Your problem is how to display the text formatted the way the user was seeing it when entering it. This is a more generic problem, and it only has to do with how HTML treats whitespace.
One approach would be to call nl2br on the comments, as Ikke says. This would replace all newlines (which the browser disregards) with <br> tags which have a visible effect on the rendered output.
Another option would be to put the text inside a <pre>...</pre> tag. This will force the browser to render it with whitespace preserved.
It's really up to what's more convenient/suitable for you.
Update: Just to be clear: do not modify the user input before inserting it in the database (unless it's part of your input validation, like e.g. stripping HTML tags from the input). Store it in an "untouched" format, and only do some processing on it before you output the data. This way, you always have the option of performing the correct processing if your output channel changes in the future (e.g. export comments to a text file vs displaying them as HTML).
you can store the comments in the same form in the mysql database. one difference would be when you retrieve the comments that has new line your code should look for \r\n and interpret it.. and also when you insert the data in mysql you will have to escape ' and \ characters from the comment.
Can anybody suggest the best way of keeping the formatting intact of a text field - ie. keeping the line breaks or carriage returns in mysql and then recognising them when the form is populated on login?
Currently when the textfield data is stored (in a TEXT field in mysql) its losing the carriage return values and adding a small square box instead.
Would it be a case of formatting with nl2br or using a str_replace instead?
Thanks
The square box is likely just not having the character set to show the paragraph marker (ΒΆ) that MySQL wants to show in place of carriage returns.
You should be able to pull the data back out of MySQL into a textarea to confirm that the data is being stored properly.
This is not a jQuery issue. The $_POST should come with the correct breaks included.
I solved my own problem. It wasn't a jQuery issue but I wasn't addressing the posted variable in the correct way.