2012-06-19 173 views
1

如何將html代碼存儲在mysql數據庫中?我可以在任何地方找到人們可以做到的地方,但沒有人說如何做。我知道我需要使用例子中使用的「mysqli_real_escape_string」去除數據,但不知道如何實際做到這一點。我簡單地放置瞭如下所示的代碼,但它只是讓我誤解了一個錯誤。我知道它是因爲HTML代碼有「」,但我需要在頁面加載時加載這些代碼,所以我如何將代碼插入到數據庫中?一個鏈接或其他東西將是驚人的。PHP MYSQL將HTML插入MYSQL數據庫

編輯:這裏是完整的代碼,它沒有輸入數據庫中的信息,但沒有給出任何錯誤。在VALUES

<?php 
    include_once('global.php'); 

    $city = ("<div id='body_sidebar'> 
     <ul> 
      <li class='sidebar_service_space'>&nbsp;</li> 
      <li class='sidebar_service_title'>About Malware</li> 
      <li><a href='<?php echo '$home' ?>/services/malware/what-is-malware.html'>What is Malware?</a></li> 
      <li><a href='<?php echo '$home' ?>/services/malware/common-signs.html'>Common Signs of Malware</a></li> 
      <li class='sidebar_service_space'>&nbsp;</li> 

      <li class='sidebar_service_title'>Malware Removal</li> 
      <li><a href='<?php echo '$home' ?>/services/malware/virus-removal.html'>Virus Removal</a> 
       <a class='tooltip' href='/knowledge_base/virus_removal.php'><img id='viruses' src='<?php print'$style_images' ?>/question.jpg' /> 
       <span class='custom info'><img src='<?php print'$style_images' ?>/info.png' alt='Information' height='48' width='48' /> 
       <em>Viruses</em> 
        The computer virus is the most infamous form of malware. It is a self replicating program that infects a system without authorization. Once installed, it will execute itself, infect system files, and attempt to propagate to other systems. The impact of a virus ranges widely from slow system performance to wiping out every file on your computer. <br /><br /> 
        <font style='text-align:right; padding-left:150px; margin-top:15px;'>Click the icon for more information...</font></span></a></li> 

      <li><a href='<?php echo '$home' ?>/services/malware/worm-removal.html'>Worm Removal</a> 
       <a class='tooltip' href='/knowledge_base/worm_removal.html'><img id='worm' src='<?php print'$style_images' ?>/question.jpg' /> 
       <span class='custom info'><img src='<?php print'$style_images' ?>/info.png' alt='Information' height='48' width='48' /> 
       <em>Worms</em> 
        A computer worm is a self-replicating computer program. It uses a network to send copies of itself to 
    other computers on the network and it may do so without any user intervention. Worms usually exploit a 
    known or zero-day vulnerability that allows them to execute their copies on computers on the same 
    network.<br /><br /> 
        <font style='text-align:right; padding-left:150px; margin-top:15px;'>Click the icon for more information...</font></span></a></li> 

      <li><a href='<?php echo '$home' ?>/services/malware/adware-removal.html'>Adware Removal</a> 
       <a class='tooltip' href='/knowledge_base/adware_removal.html'><img id='adware' src='<?php print'$style_images' ?>/question.jpg' /> 
       <span class='custom info'><img src='<?php print'$style_images' ?>/info.png' alt='Information' height='48' width='48' /> 
       <em>Adware</em> 
        Adware is a program that causes the host computer to display advertisements that the user did not request. These are frequently in the form of pop-up ads that appear while browsing the Internet and pop-under ads that open in a window hidden beneath the Internet browser's window, according to security4web.com. Another version of adware is a toolbar that attaches to an Internet browser search ability.<br /><br /> 
        <font style='text-align:right; padding-left:150px; margin-top:15px;'>Click the icon for more information...</font></span></a></li> 

      <li><a href='<?php echo '$home' ?>/services/malware/spyware-removal.html'>Spyware Removal</a> 
       <a class='tooltip' href='/knowledge_base/spyware_removal.html'><img id='spyware' src='<?php print'$style_images' ?>/question.jpg' /> 
       <span class='custom info'><img src='<?php print'$style_images' ?>/info.png' alt='Information' height='48' width='48' /> 
       <em>Spyware</em> 
        Spyware collects information from a host computer and sends it to the creator of the program. Spyware can infect a computer as a result of surfing a website that installs it or even as part of programs a user knowingly downloads to use for a different purpose, according to security4web.com. The website notes that freeware (free software) and Internet links advertising spyware-removal programs frequently install spyware.<br /><br /> 
        <font style='text-align:right; padding-left:150px; margin-top:15px;'>Click the icon for more information...</font></span></a></li> 

      <li><a href='<?php echo '$home' ?>/services/malware/rootkit-removal.html'>Rootkit Removal</a> 
       <a class='tooltip' href='/knowledge_base/rootkit_removal.html'><img id='spyware' src='<?php print'$style_images' ?>/question.jpg' /> 
       <span class='custom info'><img src='<?php print'$style_images' ?>/info.png' alt='Information' height='48' width='48' /> 
       <em>Rootkit</em> 
        Rootkits can make it difficult to discover that your computer has been infected by other types of malware. According to h-online.com, a rootkit manipulates files so they are difficult to discover on your computer. This makes it a challenge to find out if a malware process or file is running in the background on your computer because a rootkit program may be preventing you from finding it by browsing folders or searching on your computer file system.<br /><br /> 
        <font style='text-align:right; padding-left:150px; margin-top:15px;'>Click the icon for more information...</font></span></a></li> 

      <li><a href='<?php echo '$home' ?>/services/malware/trojan-removal.html'>Trojan Horse Removal</a> 
       <a class='tooltip' href='/knowledge_base/trojan_removal.html'><img id='spyware' src='<?php print'$style_images' ?>/question.jpg' /> 
       <span class='custom info'><img src='<?php print'$style_images' ?>/info.png' alt='Information' height='48' width='48' /> 
       <em>Trojan Horse</em> 
        A trojan horse is unlike a virus or a worm because it cannot replicate itself, according to spamlaws.com. Instead, it executes itself, and the execution of the file can install additional forms of malware, such as viruses or worms. It can also delete files, eliminate access to specific websites and damage files on the computer.<br /><br /> 
        <font style='text-align:right; padding-left:150px; margin-top:15px;'>Click the icon for more information...</font></span></a></li> 
      <li class='sidebar_service_space'>&nbsp;</li> 
     </ul> 
     <span id='sidebar_contactus'>Contact Us</span> 
     <span id='sidebar_findus'>Find Us</span> 
     <hr /> 
     <div id='sidebar_quote'> 
      <div id='sidebar_quote_text'>This is a small quote about someone loving who we are and what we do so they are praising us cuz well we are awesome.</div> 
     </div> 
     <div id='sidebar_quote_arrow'> 
      <div id='sidebar_quote_cite'> 
      Steven Hawkings 
      Intelectual Badass 
      <a href='#'>hiswebsite</a> 
      </div> 
     </div> 

    </div>"); 

    $city = mysqli_real_escape_string($cnx, $city); 

    /* this query with escaped $city will work */ 
    if (mysqli_query($cnx, "INSERT into services (template_name, template_code) VALUES (sidebar_malware, '$city')")) { 
     printf('%d Row inserted.\n', mysqli_affected_rows($cnx)); 
    } 

    mysqli_close($cnx); 
    ?> 
+1

你得到了什麼錯誤?此外,[大逃避主義(或:你需要知道如何處理文本中的文本)](http://kunststube.net/escapism/)。 – deceze

+0

你得到的錯誤是什麼? –

+1

*我知道我需要使用「mysqli_real_escape_string」去除數據* - 不,該函數轉義數據,它不會去除它,並且您應該使用[bound arguments](http://stackoverflow.com/a/60496/19068)而不是手動轉義。 – Quentin

回答

1

測試字符串必須用引號包裹,包裹一些報價在你的模板名稱值

if (mysqli_query($cnx, "INSERT into services (template_name, template_code) VALUES ('sidebar_malware', '$city')")) { 

查一查如何輸出MySQL的錯誤,他們會幫你調試這個問題。

-1

使用htmlentities($city);

它會轉換標籤<a>, <img>, <b>,..爲HTML實體。

+0

這完全沒有在數據庫中存儲HTML的重要性,那麼你只是將垃圾存儲在數據庫中。 – deceze

+0

這會工作,然後當你從數據庫中拉出它時,你會通過'html_entity_decode'查看http://php.net/manual/en/function.html-entity-decode.php –