如何处理PHP和PDO中的用户文本输入?

我是否应该使用PDO quote() ,然后在检索时从用户输入中除去引号和斜杠,或者有更好的方法吗?

当从PHP表单插入用户输入到MySQL时,任何单引号(撇号)都会截断数据。 我目前正在使用PDO准备好的语句和bindValue()语句。 我不确定使用PDO quote()方法,它似乎将所有的字符串封装在引号中,然后用反斜杠(“”)在字符串中将引号转义。 据我所知,如果我使用这种方法,那么当我检索数据时,我需要从字符串中去除引号。 这是最好的方法吗? 使用PDO quote()然后使用substring去除封装的单引号是否安全?

另外,我有点困惑为什么单引号会截断数据输入。 我认为PDO bindValue()应该为我的单引号转义。 我可能会误解这一点。 手册不是很具描述性。

我检查过的东西:

  • PHP魔术引号不在我的php.ini文件中,因为我使用的是版本,因为它已被折旧。
  • 阅读所有PDO手册( bindParam()bindValue()prepare()quote()
  • 在StackOverflow上阅读所有类似的问题。
  • 以下是我正在使用的PDO插入代码:

    //create query string
    $profile_update_query_text = "
    UPDATE user_profile 
    SET public=:public, headline=:headline, display_name=:display_name, skype=:skype, description=:description
    WHERE user_id=:user_id";
    
    //update table is required to modify existing user profile data
    $query_profile_update_insert = $this->db_connection->prepare($profile_update_query_text);
    $query_profile_update_insert->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
    $query_profile_update_insert->bindValue(':public', $public, PDO::PARAM_INT);
    $query_profile_update_insert->bindValue(':headline', $headline, PDO::PARAM_STR);
    $query_profile_update_insert->bindValue(':display_name', $display_name, PDO::PARAM_STR);
    $query_profile_update_insert->bindValue(':skype', $skype, PDO::PARAM_STR);
    $query_profile_update_insert->bindValue(':description', $description, PDO::PARAM_STR);
    
    //execute profile insert 
    $query_profile_update_insert->execute();
    

    我还包括用于创建PDO连接的功能,以便可以验证我没有使用会导致问题的任何设置:

    private function databaseConnection()
        {
            // connection already opened
            if ($this->db_connection != null) {
                return true;
            } else {
            // create a database connection, using the constants from config/config.php
            try {
                $this->db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
                return true;
            // If an error is catched, database connection failed
            } catch (PDOException $e) {
                $this->errors[] = MESSAGE_DATABASE_ERROR;
                return false;
            }
        }
    }
    

    如果用户输入标题如下:

    我是一个伟大的滑雪者

    在MySQL中,我得到:

    我或'我是一个伟大的滑雪者'

    取决于我是否使用PDO quote()

    PDO bindParam()的运行方式有问题,它应该是逃避单引号,还是有处理这个问题的首选方法?

    编辑 - 我检查是否使用以下语句启用了魔术引号:

    if(get_magic_quotes_gpc()){
        echo "magic quotes on";
    }else{
        echo "magic quotes off";
    }
    

    你不需要为此使用MySQL的PDO quote() 。 如果您需要将SQL插入到语句中,则无法使用bindParam() / bindValue() 。 我有一种感觉,为什么引号引起截断,这里还有其他事情正在发生。 bindParam() / bindValue()应该正确地转义你绑定的任何字符串,而不必修改或过滤它。


    我发现PDO正确地将数据插入到数据库中。 但是,当输出回HTML时,我发现我正在使用该行:

    echo "value='".$this->profile_data['headline']."'";
    

    但是,这对于来自数据库的字符串中的单引号造成了问题。

    使用使用htmlentities()解决了这个问题。

    链接地址: http://www.djcxy.com/p/93665.html

    上一篇: How to handle user text input in PHP and PDO?

    下一篇: query to PDO causes unwanted escaping