insert html tag in mysql database
Welcome back to shortlearner today we will learn how to store a html content in database.
Sometimes we need to save our html content into databse for permanent saving of our html code.
But how should we do it..?We will convert our html content into string because if your html content is too large the database would not be able to handle our large content,
So we will encode our html content by using urlencode() function by the help of urlencode() we will be able to convert our html content into a small string,
Then the string will be stored it into our database.For inserting the encoded string into database, we will create connection to our database.
Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?
Connection.php
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "database name"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>Now that the connection to our database is established we will insert our encoded creative in a table. Encode.php
<?php if(isset($_POST['submit'])) { require('connection.php'); $test=$_POST['html']; $encode= urlencode($test); $sql = "INSERT INTO `creative` (`encode`) VALUES ('$encode');"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); } ?> <!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <form method="post" action="#"> <input type="text" name="csyntax" placeholder="csyntax"><br> <input type="text" name="vsyntax" placeholder="vsyntax"><br> <textarea name="html" cols="50" rows="50"> </textarea> <br> <input type="submit" name="submit"> </form> </body> </html>Then we will fetch the stored string from the database the string will be in encoded format to decode this string we will make use of urldecode () function. The urldecode() function will return our html content which was stored in the database in encoded format. For fetching our html content which is stored in encoded format we will use select command and we will decode it using urldecode() function Decode.php
<?php require('connection.php')'; $sql = "SELECT * FROM creative"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); $encodeurl=$row['encode']; $decodeurl=urldecode ($encodeurl); echo $decodeurl; mysqli_close($conn); ?>