2012-06-19 4 views
1

어떻게 mysql 데이터베이스에 html 코드를 저장할 수 있습니까? 나는 사람들이 그것을 할 수있는 모든 곳을 찾을 수 있지만 아무도 말하지 않는다. 내가 "mysqli_real_escape_string"예제와 함께 연주를 사용하여 데이터를 스트립해야하지만 실제로 어떻게 할 생각이 없다는 것을 알아요. 아래 코드와 같이 코드를 삽입하기 만하면 오류가 발생합니다. 나는 HTML 코드가 있기 때문에 ""알고 있지만, 페이지가로드 될 때로드 된 코드가 필요하므로 어떻게 데이터베이스에 코드를 삽입합니까? 링크 또는 뭔가 놀라운 일이 될 것입니다.PHP MYSQL MYSQL 데이터베이스에 HTML을 삽입

EDIT : 전체 코드는 데이터베이스에 정보를 입력하지 않지만 오류는 발생하지 않습니다. 값에

<?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"*을 사용하여 데이터를 제거해야한다는 것을 알고 있습니다. - 아니요, 데이터를 이스케이프 처리하고 스트립하지 않으며 [바인딩 된 인수] (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

그것은 HTML 엔티티로 .. 태그 <a>, <img>, <b> 변환합니다 htmlentities($city);

사용.

+0

그건 완전히 데이터베이스에 HTML을 저장하는 지점을 놓친거야, 그럼 그냥 데이터베이스에 가비지를 저장하고 있었을거야. – deceze

+0

이것은 작동 할 것이고 데이터베이스에서 가져올 때 당신은'html_entity_decode'를 통해 실행할 것입니다. http://php.net/manual/en/function.html-entity-decode.php를보십시오. –

관련 문제