Log in

View Full Version : store php and html in mysql



sarahmx
02-22-2010, 11:27 AM
Hi,

how do i properly store my php and html code in mysql ?

ok i have text area and i would paste my php and html code inside the text area and then insert it to table

i use this but the code is not displaying correctly when i call it



<textarea rows='6' cols='40' name='content'></textarea>




$content = mysql_real_escape_string(htmlspecialchars($_POST['content']));


let say i want to paste this..



<?php echo 'hello world<br>'; ?>


this is just an example..the actual code that i want to paste is a bit long

the code is change to

<?php echo \'hello world<br>\'; ?>
in database

in browser it is display like this


\'; ?>


Help !!

Thank you in advance

sarahmx
02-22-2010, 12:34 PM
i don't know if this possible, what i'm trying to do with a help from a friend is a simple SEO content generation script... a page will pull data content from my database

we will input file name, page title, meta keywords/description and content (lot of text and some php coding) in admin side

and then automatically generate a php file based on the data from the database in a choosen web server folder with the $filename * php

i'm stuck with

1. how to add content (html & php) to the database
2. how to generate the file


what is the best way to do this ?

traq
02-22-2010, 03:24 PM
html is easy (just addslashes() to insert, stripslashes() when you pull it out). php is more complicated. There is a function called serialize() that will convert your php into a storable string, then you would unserialize() it when you pull it back out. But it's still just a string; so you'd have to use eval() to parse it as php code (which can be a security risk if you're dealing with user-submitted content in any way).

php code is *much* easier to store in an external file. Take whatever chunk of code you want to store and just put it in a separate file and include() it whenever you need.

As for generating the file, it sounds to me like you're trying to create a php template. For example, your template would look like:
template.php

<html>
<head>
<title><?php echo $title; ?>
</head>
<body>
<h1 id="header">Welcome to my site</h1>
<div id="welcome"><h2><?php echo $title; ?></h2>
<?php echo $welcomeArticle; ?>
</div>
</body>
</html>

and you would define the content like so:
webpage.php

<?php
$title = "Page title";
$welcomeArticle = "Hello world, welcome to my website's home page!";
include("/path/to/template.php");
?>

sarahmx
02-22-2010, 04:16 PM
thanks traq, you gave me a clear idea on how to implement this..

sarahmx
02-22-2010, 04:35 PM
As for generating the file, it sounds to me like you're trying to create a php template.

Yes something like that...

is it possible i would like if i click submit/save the form a file is generated/created automatically in server based on the template define and named with the name that i input for $filename....

traq
02-22-2010, 08:44 PM
so...

you want to input title/content/etc. into a form on a webpage
have the script generate a page, based on the template, using your info
?

do you want the page to be saved on the server (so anyone can visit it) or do you just want the page to be generated and served to you (gone after you leave)?

the answer is yes, either way. Saving the file to your server will be more complicated, you'll have to fix write permissions, etc., and I would *definitely* add security measures - otherwise, anyone could write anything they like to your website. Read up on php filesystem functions (http://us.php.net/manual/en/ref.filesystem.php).