Inserting correctly area to MySQL database and getting back - php

I have form in html (php). This form includes text area which is used to save log from swich, router, etc. to database.
Command line of terminal usualy have 80 cols(bytes on line) so i set the atribute cols to 80.
<tr>
<td><label for="taskText">Text:</label> </td>
<td><textarea name="taskText" id="taskText" cols="80" rows="10" required></textarea></td>
</tr>
Motivation: Why I do this? I have table in database which have this colums id (INT-autoincrement),log1(TEXT), log2(TEXT).
User save two logs into database, then I get them from database and save log1 to field $log1 and log2 to the field $log2, where $log1 will be array, where the keys will be lines.
for example:
`
$log1[0]="line1";
$log1[1]="line2";
$log1[2]="line3";`
Problem: So the problem is copy text of log to the textarea, then save it properly to the database(MYSQL) to the field of type TEXT(I choosed this hope well) and have preserved the end of lines (somehow), then load the log from the database to array, where each key will be one line (like in example now without the mark for end of line).
My solution:
Using function nl2br before saving to the database and durring loading from database use function explode:
$log1=explode("<br />", $stringWithLogLoadedFromDatabase);
But I'm not sure if this is good solution. Any other ideas?
NOTE:
I wrote there some background of my solution, but the main problem is only with end of lines of input text(log) which I´ll copy to the text area and save to the database. So I need to now where are original end of lines to be able to work with the lines.

Can you try with the following,
Use htmlspecialchars() on the string to put into the DB, and then, when pulling it back out, use htmlspecialchars_decode().
These two might make a output as per you query.

Related

Textarea content to database

I have this textarea called personalInfos where i fill the infos in following format :
<p><span class="white">1966 - '69</span><br/> text .... </p>
When i submit it to database, it gets saved ok, same format. When i retrieve the code from database to admin textarea it gets filled ok.
My only problem is on front end where i get displayed the code as text not rendered as html code. So basiclly i see it on the page like this :
<p><span class="white">1966 - '69</span><br/>
Most likely you display fetched code parsed processed by htmlentities() or similar function. This is in most cases the way to go to avoid planting i.e. html in comments. So simply stop doing this after fetching (or insert - depends where you do so) and your content will be outputed as literaly HTML and properly processed by web browser.
You should have a look at htmlspecialchars_decode()
Example
$str = '<p><span class="white">1966 - \'69</span><br/> text .... </p>';
echo htmlspecialchars_decode($str);
Also make sure to escape the single quotes as well.

Data retrieved from database ignoring line breaks

Data that is stored in my mysql database, has the breaks of text input properly, but when retrieved the data no longer has the breaks, and everything is displayed as one string without any <br>. Any idea why this would occur?
The data column is the format text,
for example: in a table there is:
hey
how
do
you
do
After retrieving the data ill echo $mesarray[0][message]; and the result is:
hey how do you do
I want the line breaks to appear, but they dont.
The reason is because HTML does not understand line breaks. They need <br /> tags to break lines.
There is a function called nl2br() which can be used to convert new lines to <br>
echo nl2br($mesarray[0][message]);

about nl, br and security while working with textarea and mysql in PHP

I'm getting data from my textarea with the following code
$about_me=mysql_real_escape_string(nl2br($_POST['about_me']));
which
1. Receives data, using $_POST.
2. nl2br makes brakes so If I echo this code to user he will see if there were new lines.
3. mysql_real_escape_string to secure code from mysql injections before entering it to database.
So if I echo this code everything works fine.
But If I edit it again through textarea, php goes to mysql gets data, puts it to textarea and I see <br> signs...
How can I get rid of them while editing my text again in textarea ?
How can I get rid of them while editing my text again in textarea ?
Stop using nl2br(), of course. It's entirely wrong here.
You use nl2br() when you want to output data that contains linebreaks to HTML, not when you want to store it in the database. Store data unchanged, format it for viewing.
If you output it into a <textarea> you don't need to use it either, since textareas display linebreaks (whereas HTML in general does not). For the textarea you need htmlspecialchars(), but apparently this is already happening - otherwise you would not see literal <br> showing up.
<?php
function br2nl($string){
$return=eregi_replace('<br[[:space:]]*/?'.
'[[:space:]]*>',chr(13).chr(10),$string);
return $return;
}
?>
Use this while getting data from database and before printing into textarea .
http://php.net/manual/en/function.nl2br.php
Check examples on this page

Post text from a text area

I have a form where a user types paragraphs into a text area and then it takes them to another page after they submit. How can I pass whatever they typed to the page after they submit? The text area might have linebreaks and if I use a query string to pass the data, it gives me an error. This is my current code to pass the field:
<?php
if(isset($_POST['form']))
{
$title = $_POST['title'];
$body = $_POST['body'];
header("SubmitForm.php?title=$title&body=$body");
?>
<html>
...html form...
It doesn't work when the text area has line breaks in it.
I would suggest installing a wysiwyg editor to make this easier for you, but i assume that would add some time for the learning curve.
The simplest tips I can give you is to set a CSS attribute for your textarea: white-space:pre so that when it gets submitted, all line breaks get sent as well.
On your server side, you would need to use the nl2br() function, so that when it gets saved on your DB or wherever you store them, all line breaks are converted to HTML breaks.
For your additional reference, I had a similar question like this last year.
You really shouldn't be putting anything that long in a query string in the first place. Look into using sessions to store data across pages instead.
(This is assuming I understood the question right)
urlencode the data in order to pass it via query string.

Send <p> tags and simple HTML formatting through a form via PHP

so I need the user to write a review about an article or book and send it to a DB via PHP but with some basic HTML formatting.. I mean, I have a form , when the user writes the review, the data is sent but without any kind of formatting, If the user want to write in a new line, the text is sent like plain text, I need to get also those new line breaks and simple stuff.
I know how to use PHP and DB connection, I just need to know how to get those new line breakes and stuff..
Use nl2br
Just before printing on the screen data from DB. It replaces \n (new line) as <br>
I recommend storing the data as plaintext, and adding the formatting on the way out. This way if you want to change the way it is formatted then you don't have to update every row in the database.
you can use nl2br() if you just need to newlines to be formatted, and a search-and-replace for anything else.
Have you considered using an existing 'plain text to markup' solution, like Markdown?
It (and others like it) allow your users to write plaintext reviews that will be sensibly formatted. (like stackoverflow uses!)
The PHP function nl2br() basically takes every new line your user enters via the form and converts the new line code to a <br> tag.
An example of using this would be:
$text = nl2br("This is text \nThis is a new line of text");
This would create the following code in your database:
This is text<br>This is a new line of text
When the user hits enter in the form textarea, PHP will pick this up as \n.

Categories