Thursday, 19 July 2018

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

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.
The HTML:
<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.

0 comments:

Post a Comment