Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store html in a mysql database

Tags:

html

php

mysql

I'm trying to store HTML in a database, so when I retrieve the form from the database, I need to show it as a form rather than text. Is there a way to do that?

This is the form

$form = "<form id='' method='' action='' class=''>
    <input type='hidden' name='my-item-id' value= $uid />
    <input type='hidden' name='my-item-name' value=$title />
    <input type='hidden' name='my-item-price' value='1' />
    <input type='hidden' name='my-item-qty' value='1' />
    <input type='submit' name='my-add-button' class='button' value='Add to cart'/>
     </form>";

at the moment when I retrieve the above from the database it shows as text,

also the form field in the database is varchar(1000)

include('adodb.inc.php');
include('adodb-pager.inc.php');
$sql = 'select title as "TITLE",description as "DESCRIPTION",form as "ADD TO CART" from mathspapers order by sysdate desc';
$pager = new ADODB_Pager($db,$sql);

$pager->Render();

Thanks

like image 306
akano1 Avatar asked Nov 23 '10 13:11

akano1


People also ask

Can I store HTML code in database?

Certainly it is possible to store html (or whatever markup or language) inside a database. Also handling that with PHP is possible. All you have to make sure is that you escape the content so that a) your code is not open to sql injection and b) the statements are valid regardless of the contents value.

How do I save HTML code in SQL database?

you can save the html codes just like text. You can use varchar(max) type column to save the html code in table. Display the code is depending the browser. But if you use nvarchar type that will cause problems in display.


2 Answers

When you are storing text in Database you probably use addslashes().
Then when you return text from Database you need something like stripslashes() before you show your HTML text. PHP Manual on stripslashes

like image 184
Dainius Avatar answered Sep 23 '22 19:09

Dainius


Instead of storing whole form pattern (and values) in the db field i can advise you to store only form field names and data (values) as key-value pairs.

You can easily use them if you store them in a standard way like ; (use at form processing page)

$form_data = "my-item-id=".$_POST["my-item-id"]."&my-item-name=".$_POST["my-item-name"]."&my-item-price=".$_POST["my-item-price"]."&my-item-qty=".$_POST["my-item-qty"];

then store $form_data only at db

to use data from db (surely after pulling $form_data from db) you can use parse_str

parse_str($form_data,$form_data_from_db);

echo $form_data_from_db["my-item-id"] will print your stored form input for instance

But these are not the my main advice. How you will build your pattern for each stored data field? Just build a function to create form like many cms does.

try this ;

function myform_pattern_1($id,$method,$action,$class,$form_data){

parse_str($form_data,$fd);

$form_html = "<form id='' method='' action='' class=''>";
$form_html .= "<input type='hidden' name='my-item-id' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-name' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-price' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-qty' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='submit' name='my-add-button' class='button' value='Add to cart'/>";
$form_html .= "</form>";
return $form_html;
}

Call this function where you like as ;

echo myform_pattern_1("form_id","post","","form_class",$form_data);

This usage has a great advantage to your method. You can change form syntax whenever you wish this way. You will -maybe- want to integrate a Jquery validation plugin later or just want to use another styling or even change syntax by adding tags you will have to change all stored db fields if you store whole form at db. Function usage is much more flexible.Also you will not use db for storing unnecessary -repeating- tags etc.

i hope you like , cys

For more information on parse_str ; http://php.net/manual/en/function.parse-str.phpenter code here

like image 36
Erdinç Çorbacı Avatar answered Sep 20 '22 19:09

Erdinç Çorbacı