douweiduo7526 2014-02-13 13:33
浏览 74
已采纳

使用预准备语句但引号未被转义或删除

I am having an issue getting some things to insert into my database. If I put quotes single or double into my text fields it will break the query and will not escape them. I just got done reading that using prepared statements eliminates the need to call mysql_real_escape_string. Can someone tell me if I am executing my query wrong. $companyInfo is an array that contains about 8 rows to be inserted.

function InsertCompanyInfo($companyInfo, $conn) {
    foreach($companyInfo as $key => $table) {           
        $keys = array_keys($table);
            $values = null;
            $x = 1;

            foreach($table as $row => $value) {
                $values .= "'$value'";
                if($x < count($keys)) {
                    $values .= ', ';

                }
                $x++;
            }

        $sql = $conn->prepare("INSERT INTO {$key} (`" . implode('`, `', $keys) . "`) VALUES ({$values});");
        $sql->execute();
        $CompanyID = $conn->lastInsertId('CompanyID');
    }
    return $CompanyID;
}

This is the error I get when I insert qoutes:

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax
 error or access violation: 1064 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 '1', 
''''"'"''"';;''';';')' at line 1' in /var/www/Survey/InsertFunctions.php:20 Stack trace: #0
 /var/www/Survey/InsertFunctions.php(20): PDOStatement->execute() #1 
/var/www/Survey/testProcess.php(8): InsertCompanyInfo(Array, Object(PDO)) #2 {main} thrown 
in /var/www/Survey/InsertFunctions.php on line 20
  • 写回答

1条回答 默认 最新

  • dongtao9887 2014-02-13 14:19
    关注

    Prepared statements work by separating the query structure and the values in code like so:

    $stmt = $pdo->prepare('INSERT INTO foo (bar) VALUES (?)');
    

    This is the query structure, which the database is given first to understand. Then you give it the values separately:

    $stmt->execute(array('baz'));
    

    What you're doing instead is you call prepare on a completely formed query which includes crudely interpolated values. There's nothing prepare can do here. The entire problem of escaping values is that the database cannot figure out what a value was and what your part of the query was after the fact. If you're giving the query fully formed and incorrectly escaped to the database, it can't magically recognise what was supposed to be what. You need to add placeholders to the query and provide the corresponding values in a separate step.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 VUE项目怎么运行,系统打不开