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
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);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.