As a PHP beginner, I know the pain of saving form values in database. But don’t worry in this tutorial I am going to to store textarea value in database.
This tutorial is for beginner you can easily learn and implement in your future web development projects.
Also Read: Insert data in Mysql using Php Form
Store textarea value in database in PHP
I will create only one input field and store in mysql database. This process involves following steps.
- Create HTML form and set method attribute value as POST.
- Apply some css to the form for better look.
- When forms submit get textarea and trim extra spaces from it.
- Save into the Mysql Database.
- If textarea saves successfully then show success msg.
Create Database:
1 2 3 |
Create database demo; |
Create Database Table:
1 2 3 4 5 6 7 8 9 |
CREATE TABLE `textarea_value` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `textarea_content` TINYTEXT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB ; |
Database Connection:
1 2 3 4 5 6 7 8 9 10 |
<?php $conn = mysqli_connect('localhost','root','','demo'); if(!$conn) { die(mysqli_error()); } ?> |
HTML:
1 2 3 4 5 6 7 8 9 10 11 |
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <label>Textarea:</label> <div> <textarea rows="10" cols="60" name="content" required></textarea> </div> <input type="submit" name="submit" value="Submit"> </form> |
In the above form, action
attribute has $_SERVER['PHP_SELF']
which means form will post on the same page. If you don’t want to use $_SERVER['PHP_SELF']
then leave action
attribute blank.
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
body{ font-family:verdana; } label{ font-weight:400; margin:10px 0px; display:block; color:#ee0000; } textarea{ border:1px solid #eeeeee; } input[type=submit]{ background:#ee0000; border:1px solid #ee0000; color:#ffffff; height:30px; display:block; margin:10px 0px; } input[type=submit]:hover{ background:#ff5858; border:1px solid #ff5858; cursor:pointer; } .success-msg{ background:#15a869; border:1px solid #15a869; color:#ffffff; width:33%; } |
Also Read: User Registration in PHP
Success Message:
1 2 3 4 5 6 7 8 9 10 |
<?php if(isset($successMsg)) { echo "<div class='success-msg'>"; print_r($successMsg); echo "</div>"; } ?> |
PHP Code on Form Submission:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php if(isset($_POST['submit'])) { $textareaValue = trim($_POST['content']); $sql = "insert into textarea_value (textarea_content) values ('".$textareaValue."')"; $rs = mysqli_query($conn, $sql); $affectedRows = mysqli_affected_rows($conn); if($affectedRows == 1) { $successMsg = "Record has been saved successfully"; } } ?> |
All Code Together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<?php $conn = mysqli_connect('localhost','root','','demo'); if(!$conn) { die(mysqli_error()); } if(isset($_POST['submit'])) { $textareaValue = trim($_POST['content']); $sql = "insert into textarea_value (textarea_content) values ('".$textareaValue."')"; $rs = mysqli_query($conn, $sql); $affectedRows = mysqli_affected_rows($conn); if($affectedRows == 1) { $successMsg = "Record has been saved successfully"; } } ?> <!DOCTYPE html> <html> <head> <title>how to store textarea value in database in php</title> <style> body{ font-family:verdana; } label{ font-weight:400; margin:10px 0px; display:block; color:#ee0000; } textarea{ border:1px solid #eeeeee; } input[type=submit]{ background:#ee0000; border:1px solid #ee0000; color:#ffffff; height:30px; display:block; margin:10px 0px; } input[type=submit]:hover{ background:#ff5858; border:1px solid #ff5858; cursor:pointer; } .success-msg{ background:#15a869; border:1px solid #15a869; color:#ffffff; width:33%; } </style> </head> <body> <?php if(isset($successMsg)) { echo "<div class='success-msg'>"; print_r($successMsg); echo "</div>"; } ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <label>Textarea:</label> <div> <textarea rows="10" cols="60" name="content" required></textarea> </div> <input type="submit" name="submit" value="Submit"> </form> </body> </html> |
https://uploads.disquscdn.com/images/97e4adf3b683e4c49fd93e89a6e137bcaac3808519dce108e9c30cded85ffbe5.png https://uploads.disquscdn.com/images/0aa89c41101f5f8a7129e43509ece450c87f0408ca596c5fca622a2e6f98d560.png .i tried your code with my code it did not work . and i wrote this code but srill the textarea tags dont insert into the database.all data are inserted expect for textare
Please also share your PHP code so i will check
it worked i have change the textarea form attribute to class=”form-control” but i do have another that ai really want your help on , how to take the data from the form store it in the database as excel sheet that when someone search for this form data it will open to him as excel ?