duanshai4484 2018-01-25 14:46
浏览 92

即使我使用html_entity_decode,html实体也会传入数据库

$string = "susan's"; //string is scraped from website
$string = html_entity_decode($string);
$sql = 'INSERT INTO database SET name = "'. $string .'"';

When I echo out $sql, it shows correct one: INSERT INTO database SET name="susan's", but when I run query it inserts susan's into database. When I run query manually from phpmyadmin it inserts correct one. Why do html entities get passed to database even when I remove them?

  • 写回答

1条回答 默认 最新

  • dongmen9517 2018-01-25 15:01
    关注

    You need to use the ENT_QUOTES flag constant.

    As per the manual:

    ENT_QUOTES Will convert both double and single quotes.
    A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENT_COMPAT | ENT_HTML401.

    Where ENT_COMPAT produces susan's.

    So your code ends up being:

    $string = htmlspecialchars_decode($string, ENT_QUOTES);
    

    Note: Depending on which API is used to insert this with, you need to be made aware that escaping it without using stripslashes() to it and should this be the case, may produce susan\'s, being another undesired result.

    Use a prepared statement, should this be coming from user input if you're not already doing so.

    This will help against an SQL injection.


    When I echo out $sql, it shows correct one: INSERT INTO database SET name="susan's",

    Tip: Before inserting into your database, always look at your HTML source. That will reveal exactly what it is that is going to be passed in the query. That is also considered as being a "tool".

    • Echo and the (HTML) source are two different animals altogether.
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看