0

我试图插入多行但在数据库中得到空行和其他错误。因此,尝试使用 implode (作为更好的选择?)但这里肯定做错了什么。

$sql = array(); 
    foreach($_POST as $key => $value ) {
        $sql[] = '("'.sqlite_escape_string($key['cust_name']).'", '.$key['address'].')';
    }

    $stmt = $db->prepare('INSERT INTO customers VALUES (cust_name, address) '.implode(','. $sql));
    $stmt->execute($sql); 
4

1 回答 1

0

执行以下操作会更好吗?

<?php

$into = "";
$values = "";

$cnt = 0;
$post_cnt = count($_POST);
foreach($_POST as $key => $value) {
    $into .= sqlite_escape_string($key).($cnt < $post_cnt && $post_cnt > 1 ? ", ": "");
    $values .= "'".sqlite_escape_string($value)."'".($cnt < $post_cnt && $post_cnt > 1 ? ", ": "");
    $cnt++;
}
$sql = "INSERT INTO customers (".$into.") VALUES (".$values.");";

$stmt = $db->prepare($sql);
$stmt->execute();
于 2013-05-20T19:49:03.367 回答