简体   繁体   中英

How to insert Data from textbox to MYSQL as HTML in PHP

I am using: <textarea rows="24" name="thetext" cols="66"></textarea>

After submitting, I send it to MYSQL in a cell type (longtext)...

For example: I input:

Hello

world

All I get in the variable is helloworld(one word).. I need the line breaks..

I tried: $results = htmlentities($_POST[thetext]); and it is not working. How can I get it to show the line breaks please?

A textarea will send new lines using \\n\\r , you need to convert that to HTML before outputting it to a page using nl2br($myVar);

In this case if you do:

echo nl2br($_POST['thetext']);

you will get Hello world with the line breaks in.

correct the implementation of text area like this

<textarea rows="24" name="thetext" cols="66"></textarea>

and use

nl2br($_POST[thetext])

instead of

htmlentities($_POST[thetext])

The easiest way is to change your rows/cells in the database to a ut8 collation charset. It should help you fix your problem.

Also the code I would use to send the form data to my database would be as follow:

$change_text = $_POST['thetext'];
$mysqli->query("YOUR_SQL_CODE");

And then to check if the data was sent correctly I would either access the row via the database directly or fetch the row like this:

if ($data = $mysqli->query("YOUR_SQL_CODE")) ;
$showtext = mysqli_fetch_row($data);
echo($showtext[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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