【问题标题】:Inserting html code in a mysql table在mysql表中插入html代码
【发布时间】:2011-01-23 04:29:42
【问题描述】:

我使用 joomla 来管理网站...我正在开发一个独立的 php 应用程序,它将插入和修改数据到 joomla 用来存储它动态创建的网页的 html 的表中...

它的工作方式是我使用 joomla 组件来创建内容,这些文章的 html 代码存储在 joomla 的表中的字段中,比如 content_table。稍后检索此 html 代码以构建一个网页。

我想对我的独立应用程序执行相同的操作...即将 html 代码添加到 content_table 中的文件中,稍后 joomla 可以检索该文件以构建页面的一部分。

问题是:html 代码当然有很多单引号和双引号,这在插入数据库时​​会造成问题。我试过 mysql_escape_string() 并且仍然出现语法错误。

我可以使用addslashes(),但由于joomla 本身稍后会检索代码,所以在稍后检索时无法使用stripslashes()....

无论如何我可以在表格的字段中添加 html 代码...

感谢您的建议...!!

编辑:添加 mysql_escape_string() 后我得到 ​​p>

Error adding details. Reason : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fulltext = '\n

这是我的查询:

UPDATE $jos_content
SET    introtext = '$intro_code',
       fulltext  = '$article_code'
WHERE  id = '$article_id'";

输入字符串如下:

 $article_code = '<hr id="system-readmore" />
<center>{loadposition user50}</center>
<p style="text-align: center;">
<span style="color: rgb(0, 255, 255);">
<i>
<b>
<span style="font-size: x-large;">
<span style="font-family: Arial;">
&nbsp;
</span>
</span>
</b>
</i>
</span>
<span style="color: rgb(0, 255, 255);">
<i>
<b>
<span style="font-size: x-large;">
<span style="font-family: Arial;">
<?php echo $title; ?>
</span>
</span>
</b>
</i>
</span>
<span style="color: rgb(0, 255, 255);">
<i>
<b>
<span style="font-size: x-large;">
<span style="font-family: Arial;">
<br />
</span>
</span>
</b>
</i>
</span>
</p>
<p style="text-align: center;">
<img height="269" width="515" border="3" 
title="<?php echo $title; ?>" 
alt=" <?php echo $title; ?>"
src="<?php echo $article_image;?>"
</p> 
<p>
<span style="font-size: small;">
<span style="font-family: Arial;">
<span style="color: rgb(153, 204, 255);">
<p style="margin-top: 2px; margin-bottom: 2px; margin-left: 120px; text-align: left;">
<i> 
<span style="color: rgb(0, 255, 0);"> 
<strong>
Cast&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :
</strong>
<b>
</b>
</span>
</i>
<span style="color: rgb(0, 255, 255);">
<b>
<?php echo $cast; ?>
</b>
</span>
<i>
<span style="color: rgb(0, 255, 255);">
<b>
<br />
</b>
</span>
</i>
<span style="font-family: Arial;">
<span style="font-size: small;">
<span style="color: rgb(153, 204, 255);">
</span>
</span>
<span style="color: rgb(0, 255, 0);">
<i>
<strong>
Direction&nbsp;&nbsp;&nbsp;
</strong>
</i>
<strong>
:
</strong>
<b>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<span class="href"
id="ctl00_ContentPlaceHolderMainContent_FormView1_Director">
<?php echo $director; ?>
</span>
</b>
</span>
</span>
<span style="font-family: Arial;">
<br />
<span style="color: rgb(0, 255, 0);">
<i>
<strong>
Production
</strong>
</i>
<strong>
:
</strong>
<b>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<?php echo $direction; ?>
</b>
</span>
<span style="color: rgb(255, 102, 0);">
<i>
<b>
<br />
</b>
</i>
</span>
<span style="font-family: Arial;">
<span style="color: rgb(0, 255, 0);">
<span style="font-family: Arial;">
<span style="font-size: small;">
<i>
<strong>
Music&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</strong>
</i>
<strong>
:
</strong>
</span>
</span>
</span>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<i>
</i>
<?php echo $music; ?>
<i>
<br />
<span style="color: rgb(0, 255, 0);">
Lyrics&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>
</i>
<span style="color: rgb(0, 255, 0);">
:
</span>
<i>
</i>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<?php echo $lyrics; ?>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<i>
<br />
</i>
<span style="color: rgb(0, 255, 0);">
<i>
Year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</i>
:
</span>
<?php echo $year; ?>
</b>
</span>
</span>
<i>
<span style="color: rgb(0, 255, 255);">
<b>
</b>
</span>
</i>
</p>
</span>
</span>
</span>
</p>
<p>
<left>
{loadposition user14}
&nbsp;
</left>
</p>
<div style="text-align: center;">
<p>
<i>
<span style="font-family: Arial;">
<b>
<span style="font-size: medium;">
<span style="color: rgb(51, 255, 255);">
Click
<img src="images/stories/Play button1.png"
alt="alt" />
in the Playlist to Download Songs
</span>
</span>
</b>
</span>
</i>
</p>
</div>
<table border="0" align="center">
<tbody>
<tr>
<td>
<h4 style="text-align: center;">
<i>
<span style="color: rgb(102, 255, 0);">
<b>
<b>
&nbsp;High Bandwidth Users
</b>
</b>
</span>
</i>
<i>
<span style="color: rgb(102, 255, 0);">
<b>
<b>
&nbsp;
</b>
</b>
</span>
</i>
<span style="color: rgb(102, 255, 0);">
<b>
</b>
</span>
</h4>
</td>
<td>
<h4 style="text-align: center;">
<i>
<span style="color: rgb(102, 255, 0);">
<b>
<b>
&nbsp;Low Bandwidth Users
</b>
</b>
</span>
</i>
<span style="color: rgb(102, 255, 0);">
<b>
<br />
</b>
</span>
</h4>
</td>
</tr>
<tr>
<td>
{auto width=&quot;235&quot; displayheight=&quot;0&quot; height=&quot;225&quot;} <?php echo $hqList; ?> {/auto}
</td>
<td>
{auto width=&quot;235&quot; displayheight=&quot;0&quot; height=&quot;225&quot;}<?php echo $lqList; ?>{/auto}
</td>
</tr>
</tbody>
</table>
<center>
{loadposition user50}
</center>';

【问题讨论】:

  • “我已经尝试了 mysql_escape_string() 并且仍然出现语法错误”。喜欢?
  • 我支持 codaddict -- 除非mysql_escape_string 函数中存在错误,否则您不应该遇到任何错误,我对此表示高度怀疑。
  • 你能打印出导致这个错误的mysql_escape_string的输入和输出吗?
  • 太长了……我还是贴吧……
  • 我认为这是个坏主意。您的应用程序以这种方式从数据库耦合到 UI。完全没有层次感。我会重新考虑,除非 PHP 强迫你做出这样的安排。

标签: php html mysql insert


【解决方案1】:

你不应该需要斜线。在正常插入期间唯一会导致问题的是引号,mysql_escape_string() 应该处理除字符集问题之外的问题。也可以试试mysql_real_escape_string()

另外,请注意,将用户提供的原始 HTML 存储在数据库中可能会导致安全问题。考虑改用 bbcode 或 markdown 之类的东西。

【讨论】:

  • BBcode 在这里不是一个选项。因为 joomle 只是获取字段中的条目并将其输出到页面中......!!它不做任何类型的解析...
【解决方案2】:

确认一下,您的查询如下所示:

$query = '
    UPDATE "'.mysql_real_escape_string ($jos_content).'"
    SET    introtext = "'.mysql_real_escape_string ($intro_code).'",
           fulltext  = "'.mysql_real_escape_string ($article_code).'"
    WHERE  id = "'.mysql_real_escape_string ($article_id).'"
";

【讨论】:

    【解决方案3】:

    嗯..调试它..原来问题毕竟不是转义功能...

    查看查询:

    UPDATE $jos_content
    SET    introtext = '$intro_code',
           fulltext  = '$article_code'
    WHERE  id = '$article_id'";
    

    你可以看到'fulltext'字段......显然,“fulltext”这个词是一个mysql关键字......准确地说,它是一个字段类型,如TEXT,INT,MEDIUMTEXT等......

    我把查询改成了这个

    "UPDATE $jos_content
    SET    $jos_content.introtext = '$intro_code',
           $jos_content.fulltext  = '$article_code'
    WHERE  $jos_content.id = '$article_id'";
    

    瞧……!!!!

    【讨论】:

    • 确保你的数据类型是 nvarchar
    • 其实不是字段类型。它是一种索引。我相信您也可以通过将字段名称括在反引号中来避免此问题 (`)
    【解决方案4】:

    我更喜欢在插入数据库之前将代码转换为普通字符串。我认为,这是最安全的情况。考虑使用此代码:

    $article_code = base64_encode($article_code);
    /* insert to database */
    

    因此,当您想使用该代码时,只需使用 base64_decode 进行解码。我建议您使用 'text' 数据类型来保存 $article_code 而不是 'varchar'。

    【讨论】:

    • 你说得对,唯一的缺点是这会使你的数据增加 33% 以上,但就目前而言,这是一个 KISS 解决方案!
    • 现在您的数据库无法搜索。
    【解决方案5】:

    fulltext 是 mysql 预定义关键字。请使用 Acute(`) 或单引号(')

    这里是代码 -

    UPDATE $jos_content
    SET    `introtext` = '$intro_code',
           `fulltext`  = '$article_code'
    WHERE  `id` = '$article_id'";
    

    【讨论】:

      【解决方案6】:

      我遇到了同样的问题,我用正则表达式修复了它。 你可以使用这样的东西:$target = '{~p class={{q}}important-text{{q}}~}Some text here {~/p~}';

      然后使用preg_replace()函数:

      class handle  
      { 
        public static function makehtml($target)   
        {
          $output = preg_replace("#{~#", "<", $target);
          $output = preg_replace("#~}#", ">", $target);
          $output = preg_replace("#{{q}}#", '"', $target);  
          return $output;
       }  
      }  
      echo handle::makehtml($target);
      // output : <p class="important-text">Some text here</p>
      

      【讨论】:

        【解决方案7】:

        如果您担心空间问题并使用此处发布的 base 64 编码方法,您可以使用 php 中的 gzdeflate 命令将其缩减,然后对其进行编码。这是一些生成的示例字符的小测试脚本。显然,如果您关心大小,则可以研究不同的压缩方法,但这意味着您可以将其放入 mysql 中,以最小化 db 上的大小。然后你可以用 gzinflate(base64_decode( ... )); 读回它。

        <?php
        //generate sample chars for the example
        function generateRandomString($length = 10000) {
            $characters = '01234"56/789abcdefghij:klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $randomString = '';
            for ($i = 0; $i < $length; $i++) {
                $randomString .= $characters[rand(0, strlen($characters) - 1)];
            }
            return $randomString;
        }
        //
        
        $string =generateRandomString();
        echo 'string size:'.strlen($string);
        $b64html = base64_encode($string);
        echo '<br/>'.'Original base64 size:'.strlen($b64html);
        $compressed = gzdeflate($string,  9);
        echo '<br/>'.'compressed size:'.strlen($compressed);
        echo '<br/>'.'base64 compressed size:'.strlen(base64_encode($compressed));
         /* insert into db the base64_encode($compressed); */
        ?>
        

        【讨论】:

          【解决方案8】:

          调用 mysql_escape_string() 函数传入保存 html 文本的变量,例如:

          mysql_escape_string($_POST["text"]);

          会保证文本中的引号等特殊字符不会引起php错误,数据库会更新成功。

          【讨论】:

            【解决方案9】:

            这是我找到的最好的方法addslashes()

            $article_code = addslashes($article_code);
            
            UPDATE $jos_content
            SET    introtext = '$intro_code',
                   fulltext  = '$article_code'
            WHERE  id = '$article_id'";
            

            【讨论】:

              【解决方案10】:

              将html代码插入数据库

              $title = $_POST['title'];
              $code = '<h1>hello</h1></p>this html tage</p>';
              $htmlcode = addslashes($code);
              
               $query = "INSERT INTO `template` (`title`,`code`) VALUES ('".$title."','".$htmlcode."');"; 
              mysqli_query($con,$query);
              

              将数据插入数据库后,在列表页面中显示数据

              $result = mysqli_query($con,'SELECT * FROM template');                                  
              while($row = mysqli_fetch_assoc($result)) {
              
               echo htmlentities($row['code']);
              
              }
              htmlentities($row['code']); //here your row name;
              

              【讨论】:

                【解决方案11】:

                如果你使用 mysqli,它需要一个连接。

                mysqli_real_escape_string($conn, $_POST["name"]);
                

                【讨论】:

                  猜你喜欢
                  • 2011-11-21
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-08-07
                  • 2017-03-30
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多