Hi friends, In this tutorial, I will explain how to store textarea value in a database using PHP.
Storing textarea values in a database is a common requirement in our web development projects. It's common to allow users to input large text data using textarea. Once the users submit their data, we are required to save it in our database for future processing of our web project.
Friends this tutorial is very easy, I'll walk you through the process of storing textarea values in a database through PHP script.
Create New ProjectÂ
Check out my project directory structure. I configured this project inside xampp/htdocs folder.
xampp/
+-- htdocs/
+-- Save-Textarea-Value-Database-PHP/
+-- dbconfig.php
+-- index.php
If you have WampServer install this project inside wamp/www folder.
wamp/
+-- www/
+-- Save-Textarea-Value-Database-PHP/
+-- dbconfig.php
+-- index.php
Creating a Database Table
I have created the user name table in my MySQL database.
CREATE TABLE tbl_user (
id int,
user_address varchar(255)
);
Connect the Database
This is "dbconfig.php" file. In this file, I establish the MySQL database connection through the PDO extension.
<?php
$db_host="localhost";
$db_user="root";
$db_password="";
$db_name="textarea_db";
try
{
$db=new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOEXCEPTION $e)
{
$e->getMessage();
}
?>
Create an HTML Form with a textarea Element:
This is the "index.php" file in this file I created a simple HTML form with a <textarea> field and a submit button.
I have applied the HTTP POST method in the form tag to communicate the textarea value to the server.
<form method="post" class="form-horizontal">
<div class="form-group">
<label class="col-sm-3 control-label">Address</label>
<div class="col-sm-6">
<textarea name="txt_address" class="form-control" rows="5" placeholder="enter your address"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6 m-t-15">
<input type="submit" name="btn_save" value="Save" class="btn btn-success btn-lg btn-block" >
</div>
</div>
</form>
<?php
if(isset($errorMsg))
{
?>
<div class="alert alert-danger alert-dismissible">
<strong>WRONG ! <?php echo $errorMsg; ?></strong>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php
}
if(isset($insertMsg))
{
?>
<div class="alert alert-success alert-dismissible">
<strong>SUCCESS ! <?php echo $insertMsg; ?></strong>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php
}
?>
Look at the below form shows a User Interface of this type:
Store textarea value in Database
Look below, I implemented the PHP script that handles our form-submitted requests and stores the textarea value in the database.
This PHP code has been made separately within the index.php file that starts above the <!DOCTYPE html> tag.
<?php
require_once "dbconfig.php";
if(isset($_REQUEST['btn_save']))
{
$address = $_REQUEST['txt_address']; //get textarea value "txt_address"
if(empty($address)){
$errorMsg="Please Enter Your Address";
}
else
{
try
{
if(!isset($errorMsg))
{
$insert_stmt=$db->prepare('INSERT INTO tbl_user(user_address) VALUES(:uadd)'); //sql insert query
$insert_stmt->bindParam(':uadd',$address); //bind insert query parameter of :uadd
if($insert_stmt->execute())
{
$insertMsg="textarea value save successfully in database........"; //run query success message
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
No comments:
Post a Comment