PHP Tips & Tutorials SQL / MySQL Tips and Tutorials

MySQL, PHP: Store form textarea value or data to MySQL database table

To send the textarea text value in any HTML forms to the web server and store it into a MySQL table by PHP, you will need to POST the textarea data to a PHP script and then the PHP script would connect to a MySQL database to put the data into one of the tables by inserting a record or updating one.


<textarea rows="10" cols="10" name="bio"></textarea>

Now the visitor has finished typing in his text and clicked the submit button. The data is then transmitted via POST method to a PHP script:

$data = $_POST['bio']; // because the name attribute of the textarea tag is 'bio'
$data = mysql_real_escape_string[$data]; // necessary step before using the data string to prevent SQL injection attacks
mysql_query("INSERT INTO `authors` (`bio`) VALUES ('$data')");

By committing the last SQL query, the bio data entered by the visitor is successfully inserted into the table `authors` as a new record. Other than this, you may also want to use:

UPDATE `authors` SET `bio` = '$data' WHERE ... 

To update an existing row with the textarea data instead of inserting a new one.

I think that’s pretty much what the basics look like to put textarea value into a database.

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Shoot me a message.

5 replies on “MySQL, PHP: Store form textarea value or data to MySQL database table”

For some reason your use of backticks here prompted me to fix the problem I was having. Thanks!

If you dnot want to allow the user to enter the html code in the textarea then you can use the PHP function [ strip_tags() ] to remove the thml tags from the text entered.

This function will also remove the javascript.

the database is not updating but it dont shows any error???////

$index= $_POST["textarea2"];
$id = $_POST["textarea1"];
$name = $_POST["textarea"];

echo "'$index'";
echo "'$id'";
echo "'$name'";
echo "”;
$sql = “UPDATE sunhill_first_lease.about_us SET about_us.description = ‘$name’ where about_us.head =’$index’ and about_us.title=’$id’ “;



Comments are closed.