Bravat Posted April 10, 2011 Share Posted April 10, 2011 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 More sharing options...
Jnerocorp Posted April 10, 2011 Share Posted April 10, 2011 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 https://forums.phpfreaks.com/topic/233258-php-code-inside-mysql-db/#findComment-1199666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.