【问题标题】:Insert large amount of variables into table using PDO使用 PDO 将大量变量插入表中
【发布时间】:2012-11-11 16:20:48
【问题描述】:

我有一个包含大约 25 个输入字段的大表单。

我正在尝试将它们插入我的表格中,而我知道如何使用以下内容的唯一方法...

$count = $dbh->exec("INSERT INTO directory(field1, field2) VALUES (':value1', ':value2')");

由于我有这么多的帖子变量,有没有比将每个人都输入到我的查询中更好的方法来做到这一点?

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    动态准备查询

    您可以从 $_POST 数组动态构建查询:

    但是,永远不要相信用户输入,这意味着您不能相信 $_POST 中的数据将包含有效的列名。

    1.清理帖子数据

    您可以定义一个列入白名单的列名数组$whitelist = array('field1', 'field2', ...),然后使用:

    $data = array_intersect_key($_POST, array_flip($whitelist));
    

    查找列入白名单的列与您的 $_POST 数组之间的交集。 (感谢@BillKarwin)

    2。构建查询

    private function buildInsertSql($data, $table) {
        $columns = "";  
        $holders = "";  
        foreach ($data as $column => $value) {  
           $columns .= ($columns == "") ? "" : ", ";  
           $columns .= $column;  
           $holders .= ($holders == "") ? "" : ", ";  
           $holders .= ":$column";  
        }  
        $sql = "INSERT INTO $table ($columns) VALUES ($holders)";  
        return $sql; 
    }
    

    这将为您提供以下形式的 SQL 语句:

    $sql = INSERT INTO directory (field1, field2) VALUES (:field1, :field2)
    

    并准备声明:

    $stmt = $dbh->prepare($sql);
    

    3.绑定参数

    然后您可以将参数动态绑定到占位符:

    foreach ($data as $placeholder => $value) {
        $stmt->bindValue(":$placeholder", $value);
     }
    

    并执行它:

    $stmt->execute();
    

    更高级一点...

    【讨论】:

    • array_intersect_keys 正在给Uncaught Error: Call to undefined function array_intersect_keys() in。将其更改为array_intersect_key
    【解决方案2】:

    您可以动态构建 INSERT 语句,但您应该小心限制 POST 字段。不要相信请求只包含有效的列。

    还分隔表和列标识符。 MySQL 默认使用反引号作为标识符分隔符。

    function execInsert($pdo, $table, $_POST) {
        // get a list of columns in $table, either by hard-coding them per table, 
        // or by querying DESC or INFORMATION_SCHEMA
        $real_columns = array('col1', 'col2', 'col3');
    
        $fields = array_intersect_key($_POST, array_flip($real_columns));
        if (!$fields) {
            // no POST fields match the real columns
            return false;
        }
    
        $columns = array_map(function($col) { return "`".$col."`"; }, array_keys($fields));
        $holders = array_map(function($col) { return ":".$col; }, array_keys($fields));
        $values = $fields;
    
        $sql = "INSERT INTO `$table` (" . join(",", $columns) . 
            " VALUES (" . join(",", $holders) . ")";  
    
        if (($stmt = $pdo->prepare($sql)) === false) {
            die(print_r($pdo->errorInfo(), true));
        }
        if (($retval = $stmt->execute($values)) === false) {
            die (print_r($stmt->errorInfo(), true));
        }
        return $retval;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2012-12-28
      相关资源
      最近更新 更多