我试图插入多行,但在数据库和其他错误中得到空行。 那么,试图使用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);
答案 0 :(得分: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();