Jump to content

PHP code inside MySQL DB


Bravat

Recommended Posts

How can I insert PHP code inside MySQL DB? I have mixed HTML and PHP code like this:

<div class="rightpanel">

<div class="hltred">

<div class="hlthead">

<p><?php echo $pages->naslov; ?></p>

</div>

<div class="hltcontent">

<p>

ovde treba da ide loop sa svi podlinkovi za taj glavni koji je aktivan</p>

</div>

</div>

</div>,

but when i insert it into DB i get this code (I use htmlspecialchars function to store):

<div class="rightpanel">

<div class="hltred">

<div class="hlthead">

<p>

<!--?php echo $pages---> naslov; ?></p>

</div>

<div class="hltcontent">

<p>

ovde treba da ide loop sa svi podlinkovi za taj glavni koji je aktivan</p>

</div>

</div>

</div>

Link to comment
https://forums.phpfreaks.com/topic/233258-php-code-inside-mysql-db/
Share on other sites

I think something like this might work:

 

<?php

function enc2db($str2encode, $pagename, $filetype) {
$fixed = htmlspecialchars($str2encode);
mysql_query("INSERT INTO example (id, pagename, filetype, contents) VALUES('', '$pagename', '$filetype', '$fixed' ) ") or die(mysql_error()); 
}


function decfrmdb($pagename, $filetype) {

$result = mysql_query("SELECT * FROM example WHERE pagename='$pagename' AND filetype='$filetype'") or die(mysql_error);
while($row = mysql_fetch_array( $result )) {

$page_new = "".$row['pagename']."".$row['filetype']."";
$fixed_new = htmlspecialchars_decode($row['contents'];

}

}

$str2encode = 'div class="rightpanel">
   <div class="hltred">
      <div class="hlthead">
         <p><?php echo $pages->naslov; ?></p>
      </div>
      <div class="hltcontent">
         <p>
            ovde treba da ide loop sa svi podlinkovi za taj glavni koji je aktivan</p>
      </div>
   </div>
</div>,
but when i insert it into DB i get this code (I use htmlspecialchars function to store):
<div class="rightpanel">
   <div class="hltred">
      <div class="hlthead">
         <p>
<!--?php echo $pages--->            naslov; ?></p>
      </div>
      <div class="hltcontent">
         <p>
            ovde treba da ide loop sa svi podlinkovi za taj glavni koji je aktivan</p>
      </div>
   </div>
</div>';

echo enc2db($str2encode, example, .php); // Will encode the given string
echo "<br><br>";
echo decfrmdb(example, .php);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.