【问题标题】:special characters break mysql insert特殊字符打破mysql插入
【发布时间】:2012-04-25 09:57:29
【问题描述】:

我有一个这样的mysql语句:

mysql_query("INSERT INTO movies (comments, description, synopsis)
VALUES ('$_POST["comments"]', '$_POST["desc"]',$_POST["synopsis"])");

如您所见,非常简单明了。问题是当我在表单中输入特殊字符时,它不会将数据插入到我的表中(使用 phpmyadmin 直接检查是否已插入)。 例如,如果我在 cmets textarea 中输入此值:“这是一条评论” 这行得通 如果我改为:“你叫什么名字?:John doe 是我的名字” 它坏了。我知道它是因为 mysql 使用了这些字符......关于我应该做什么的任何建议?

【问题讨论】:

标签: php mysql html-sanitizing input-sanitization


【解决方案1】:
$comments = mysql_real_escape_string($_POST['comments']);
$desc = mysql_real_escape_string($_POST['desc']);
$synopsis = mysql_real_escape_string($_POST['synopsis']);

mysql_query("INSERT INTO movies (comments, description, synopsis)
VALUES ('$comments', '$desc', '$synopsis'");

【讨论】:

    【解决方案2】:
    $comments = mysql_real_escape_string($_POST['comments']);
    $desc = mysql_real_escape_string($_POST['desc']);
    $synopsis = mysql_real_escape_string($_POST['synopsis']);
    
    mysql_query("INSERT INTO movies (comments, description, synopsis)
    VALUES ('$comments', '$desc', '$synopsis'");
    

    更多信息google“php addlashes”,或者看这个页面看解释http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

    【讨论】:

    • 为什么是addslashes而不是相关的mysql函数?
    • 更改为“mysql_real_escape_string”。出于这个原因,我放了“google for php addlashes”只是为了指导更多信息。
    【解决方案3】:
    mysql_query("INSERT INTO movies (comments, description, synopsis) 
    VALUES ('".mysql_real_escape_string($_POST["comments"])."', '".mysql_real_escape_string($_POST["desc"])."','".mysql_real_escape_string($_POST["synopsis"])."'"); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2012-10-17
      • 2016-04-16
      • 2016-11-24
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多