Home >>MySQL Tutorial >How to insert data from textarea to database PHP

How to insert data from textarea to database PHP

How to insert data from textarea to database PHP

Create HTML Form(four fileds: email,textaerea,save button and display button) Create a database demo. crate a database table "textarea" with three columns:id,email,message. Connect php with mysqli. Enter your emailid and data from HTML Form, to save in textarea table click on save button. After insertion if you want to display data then click on display button, data will displayed in tabular format.

Create Database and Table

//create a database demo
CREATE DATABASE `demo`;
USE demo;

//create a table textarea
CREATE TABLE `textarea`
 (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`email` VARCHAR( 50 ) NOT NULL ,
`message` TEXT NOT NULL ,
UNIQUE (
`email`
)
) ENGINE = MYISAM ;

PHP Script

<?php

//mysqli connectivity, select database
$connect= new mysqli("localhost","root","","demo") or die("ERROR:could not connect to the database!!!");

//extract all html field
extract($_POST);
if(isset($save))
{
//store textarea values in <pre> tag
$msg="<pre>$a</pre>";

//insert values in textarea table
$query="insert into textarea values('','$e','$msg')";
$connect->query($query);
echo "Data saved";	

}

//click on display button to show all values entered by you
if(isset($disp))
{
	$query="select * from textarea";
	$result=$connect->query($query);
	echo "<table border=1>";
	echo "<tr><th>Email</th><th>Message</th></tr>";
	while($row=$result->fetch_array())
		{
		echo "<tr>";
		echo "<td>".$row['email']."</td>";
		echo "<td>".$row['message']."</td>";
		echo "</tr>";
		}
	echo "</table>";	
}
?>

HTML Form

<style>
input,textarea{width:250px}
textarea{height:200px}
input[type=submit]{width:150px}
</style>
<form method="post">
<table width="200" border="1">
  
  <tr>
    <td>Email</td>
    <td><input type="email" name="e" placeholder="email" /></td>
  </tr>
 
  
  <tr>
    <td>Message</td>
    <td><textarea placeholder="contents"  name="a"></textarea></td>
  </tr>
  <tr>
    <td colspan="2">
		<input type="submit" value="Save" name="save"/>
		<input type="submit" value="Display" name="disp"/>
	</td>
  </tr>
  
</table>
</form>

No Sidebar ads