我试图用一个关联数组bindParams,循环通过foreach,但我不知道它以某种方式工作.我收到此错误:
SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配
这是代码.var $columns也$values来自数组,被操纵,所以它接受它们就像x, x, x和:x, :x, :x
$stmt1 = $conn->prepare("INSERT INTO data($columns)
VALUES ($values)");
foreach ($array as $key => $value)
{
$key = ":" . $key;
$stmt1->bindParam($key, $value);
$stmt1->execute();
}
Run Code Online (Sandbox Code Playgroud)
我很确定这些列正在对数组中的键进行管理.
编辑:更多的代码
$values = '';
$columns = implode(',', $array);
foreach($array as $key)
{
$values .= ":" . $key . ",";
}
$values = substr($values , 0, -1);
Run Code Online (Sandbox Code Playgroud)